1

Okay, so this is pretty weird; before upgrading to the final GMseed of Xcode my buttons worked perfectly fine... Now I can't even do them on a super simple scale without the program crashing on click.

The absolute most bizarre aspect is that it works SOME of the time, but like 85%, it just crashes; and when it does work, it'll crash after extensive use.

Here is my code (stripped down to the simplest button implementation I could think of):

@interface TESTViewController ()
{
    UIButton *button;
}

@end

@implementation TESTViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addButton];
    [button addTarget:self action:@selector(buttonPressed) 
    forControlEvents:UIControlEventTouchDown];
}

- (void)addButton
{
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];

    [button setTitle:@"CLICK" forState:UIControlStateNormal];

    [button setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:button];
}

- (void)buttonPressed
{
    NSLog(@"WORKED");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];


    TESTViewController *test = [[TESTViewController alloc] init];

    [self.window addSubview:test.view];

    return YES;
}

I've tried creating the button in the init function, viewDidLoad -- neither works consistently, though viewDidLoad seems to work a little more frequently...

I tried -(void) addButton:(UIButton*)button as well Using:

[button addTarget:self action:@selector(buttonPressed:) 
        forControlEvents:UIControlEventTouchDown]; 

--No difference.

I am at a total loss. I tried looking it up here on the forums, but unfortunately most of it refers to programming with IBActions.

The error I get is bad access So my guess is that when the button is clicked, it's not actually accessing the buttonPressed function but something else entirely... I have no clue as to why this would be the case...

Thanks for any help!

4

5 に答える 5

3

問題は「didFinishLaunchingWithOptions」メソッドにあります。ここでは、「TESTViewController *test = [[TESTViewController alloc] init]」というように、割り当てを解除されるローカル オブジェクトを作成しています。このオブジェクトは保持しておく必要があるため、プロパティにして「self.test = [[TESTViewController alloc] init]」を使用してみてください。それが動作します。また、通常は、viewController を Windows のサブビューに追加するのではなく、rootViewController を使用する必要があります。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.test = [[TESTViewController alloc] initWithNibName:nil bundle:nil];
    self.window.rootViewController = self.test;
    [self.window makeKeyAndVisible];
    return YES;
}
于 2013-09-24T05:29:59.527 に答える
0

これがクラッシュの原因かどうかはわかりませんが、アクション ( などbuttonPressed) には送信者パラメーターが必要です。

[button addTarget:self action:@selector(buttonPressed:) 

- (void)buttonPressed:(id)sender
{
...
}
于 2013-09-24T05:24:00.320 に答える
-1

プログラムでボタンを定義するには、buttonWithTypeメソッドを使用してください。これを試してください

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(buttonPressed)
 forControlEvents: UIControlEventTouchUpInside];//Any control event type
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 80, 45);
[view addSubview:button];
于 2013-09-24T05:01:14.460 に答える