Iphone UIButton在单击时崩溃
好吧,这很奇怪;在升级到Xcode的最后一个GMseed之前,我的按钮工作得非常好。。。 现在我甚至不能在一个超级简单的规模上做这些事情,而不让程序在点击时崩溃 绝对最奇怪的是,它在某些时候可以工作,但像85%一样,它只是崩溃了;当它工作时,在广泛使用后会崩溃 下面是我的代码(精简到我能想到的最简单的按钮实现): 我尝试过在init函数中创建按钮viewDidLoad——虽然viewDidLoad似乎工作得更频繁一些,但这两种方法都不一致 我也尝试了Iphone UIButton在单击时崩溃,iphone,ios,objective-c,uibutton,Iphone,Ios,Objective C,Uibutton,好吧,这很奇怪;在升级到Xcode的最后一个GMseed之前,我的按钮工作得非常好。。。 现在我甚至不能在一个超级简单的规模上做这些事情,而不让程序在点击时崩溃 绝对最奇怪的是,它在某些时候可以工作,但像85%一样,它只是崩溃了;当它工作时,在广泛使用后会崩溃 下面是我的代码(精简到我能想到的最简单的按钮实现): 我尝试过在init函数中创建按钮viewDidLoad——虽然viewDidLoad似乎工作得更频繁一些,但这两种方法都不一致 我也尝试了-(void)addButton:(UIBut
-(void)addButton:(UIButton*)按钮
使用:
--没有区别
我完全不知所措。我试着在论坛上查找它,但不幸的是,大部分内容都涉及到使用iBaction进行编程
我得到的错误是访问错误
所以我猜当点击按钮时,它实际上并没有访问buttonPressed函数,而是完全其他的东西。。。我不知道为什么会这样
谢谢你的帮助 我想您的按钮对象有内存泄漏。您是使用ARC还是手动内存管理?如果是手动的,请确保保留它。如果是ARC,请使用属性,编译器将完成其余工作
@property (nonatomic, strong) UIButton *button;
这样试试看
- (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中的这一行要以编程方式定义按钮,请使用
按钮WITHTYPE
方法,尝试以下操作
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];
我不确定这是否是导致崩溃的原因,但操作(如按下
按钮
)应该有一个发送方参数:
[button addTarget:self action:@selector(buttonPressed:)
- (void)buttonPressed:(id)sender
{
...
}
问题在于“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;
}
为什么不使用
UIControlEventTouchUpInside
?我不认为为relase Button对象发出的uicontrol事件的问题只是给出UIBUtton
属性和Systhsize
并使用Self.Button阻止控制台中显示的错误。还可以使用“启用僵尸对象”来获取释放的实例从“编辑方案”中的“诊断”选项卡。这可能会有帮助。
[button addTarget:self action:@selector(buttonPressed:)
- (void)buttonPressed:(id)sender
{
...
}
- (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;
}