UIButton在点击时崩溃

时间:2013-09-24 04:54:13

标签: iphone ios objective-c uibutton

好的,所以这很奇怪;在升级到Xcode的最终GMseed之前,我的按钮工作得很好...... 现在我甚至无法在超级简单的比例下完成它们而不会使程序在点击时崩溃。

绝对最离奇的方面是,它的工作时间有点,但是像85%一样,它只会崩溃;当它确实有效时,它会在大量使用后崩溃。

这是我的代码(根据我能想到的最简单的按钮实现):

@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;
}

我已经尝试在init函数中创建按钮viewDidLoad - 虽然viewDidLoad似乎工作得更频繁,但是两者都不能正常工作......

我也试过-(void) addButton:(UIButton*)button 使用:

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

- 没有区别。

我完全失去了。我尝试在论坛上查找它,但遗憾的是大多数都是指使用IBActions进行编程。

我得到的错误是访问不当 所以我的猜测是,当点击按钮时,它实际上并没有访问buttonPressed功能,而是完全没有其他东西......我不知道为什么会出现这种情况......

感谢您的帮助!

5 个答案:

答案 0 :(得分:3)

问题在于你的“didFinishLaunchingWithOptions”方法。在这里,您将创建一个获取Deallocated的本地对象,即“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;
}

答案 1 :(得分:1)

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

@property (nonatomic, strong) UIButton *button;

答案 2 :(得分: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的这一行

答案 3 :(得分:0)

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

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

- (void)buttonPressed:(id)sender
{
...
}

答案 4 :(得分:-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];