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]”。它会起作用的。按照惯例,您应该使用 rootViewController 而不是将您的 viewController 添加到 windows 的子视图中。

- (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 回答
1

我认为您在该 Button 对象上有内存泄漏您是使用 ARC 还是手动内存管理?如果手动确保您保留它。如果是 ARC,则使用属性,编译器将完成剩下的工作

@property (nonatomic, strong) UIButton *button;
于 2013-09-24T04:58:29.367 回答
0

我不确定这是否是导致崩溃的原因,但操作(如您的buttonPressed)应该有一个 sender 参数:

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

- (void)buttonPressed:(id)sender
{
...
}
于 2013-09-24T05:24:00.320 回答
0

试试这种方式

- (void)addButton
{
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
    [button addTarget:self action:@selector(buttonPressed) 
                 forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"CLICK" forState:UIControlStateNormal];

    [button setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:button];
}

并删除

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

来自 viewDidLoad 的这一行

于 2013-09-24T04:58:31.847 回答
-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 回答