UIButton崩溃的应用程序触摸
我有一个导航控制器,其中包含一些按钮的视图,但是当我按下按钮时,我得到一个EXC_BAD_ACCESS错误。因为目标设置正确,我不能认为我做错了。它崩溃了按钮是以编程方式添加还是通过IB添加。UIButton崩溃的应用程序触摸
按钮代码:
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 incase,以任何方式帮助。
- (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;
}
我也遇到这个错误;我一直在使用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");
}
一切工作正常...因此,尝试的是,如果你仍然有问题。 祝你好运!
谢谢,这是一段时间以前,我不记得我做了什么使它工作! – NinjaLikesCheez 2013-11-11 16:06:56
您的代码中没有任何内容似乎指向任何问题。 Use this tutorial使Xcode在所有例外情况下中断。这会让你更接近'犯罪现场',而不是撞上主力。
现在就做,谢谢! – NinjaLikesCheez 2012-04-11 15:42:05
尝试放置断点并让我们知道它崩溃的位置 – 2012-04-11 15:33:04
我应该在哪里放置它们?该应用程序崩溃时按下按钮,其余的代码只是添加各种其他UI的东西子视图。 – NinjaLikesCheez 2012-04-11 15:35:28