繁体   English   中英

触摸UIButton崩溃的应用程序

[英]UIButton crashing app on touch

我有一个navigationController,其中包含一个带有一些按钮的视图,但是当我按下一个按钮时,我得到一个EXC_BAD_ACCESS错误。 我不能认为我做错了什么,因为目标设置正确了。 无论是通过编程方式添加按钮还是通过IB添加按钮,都会崩溃。

按钮代码:

UIButton *reportsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
reportsButton.frame = CGRectMake(20, 100, 100, 50);
[reportsButton setTitle:@"Reports" forState:UIControlStateNormal];
[reportsButton addTarget:self action:@selector(reportsButtonTouched:) forControlEvents:UIControlEventTouchUpInside];

功能按钮正在尝试访问:

- (void)reportsButtonTouched:(id)sender
{
    NSLog(@"working");
}

错误:

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //EXC_BAD_ACCESS code=1
}

按钮尝试访问的功能存在。

也许这是我不知道的NavigationController功能的方式,但是我之前做过这个没有任何问题。

感谢您提供任何答案,我真的很感谢以前从此​​站点获得的帮助。

编辑:这是我的AppDelegates didFinishLaunching情况,以任何方式提供帮助。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    UIViewController *homevc = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:homevc];

    [self.window addSubview:nav.view];
    [self.window makeKeyAndVisible];
    return YES;
}

您的代码中似乎没有任何内容指向任何问题。 使用本教程可以使Xcode打破所有异常。 这将使您更接近“犯罪现场”而不是崩溃。

我也遇到了这个错误。 我一直在使用SpriteKit进行编程,并以某种方式实现我的按钮,然后当我回到不使用框架的时候,突然我实现的每个按钮都导致了严重的访问错误。

我发现我正在初始化viewDidLoad中的按钮,因为我一直在使用didMoveToView,而我不小心将它们视为相同的函数(说实话,我对viewDidLoad的实际作用并不十分了解)。

但是当我这样做时:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
    // Custom initialization
    [self addButton];

}
return self;
}


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

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


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

[self.view addSubview:button];
}

- (void)buttonPressed:(UIButton *)button
{
    NSLog(@"WORKED"); 
}

一切正常,...如果您仍然遇到问题,请尝试一下。 祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM