当我点击它时,为什么这个按钮会崩溃我的应用程序? UIButtion iPhone(ARC)
问题描述:
每当我点击“newButton”我的应用程序崩溃。我正在使用自动引用计数。当我点击它时,为什么这个按钮会崩溃我的应用程序? UIButtion iPhone(ARC)
编辑:刚试过这个在不同的应用程序,它的工作原理,但不工作在我自己的。
这里是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *fullView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 480)];
fullView.backgroundColor = [UIColor blackColor];
[[self view] addSubview:fullView];
UIImage* blackButton =[[UIImage imageNamed:@"UIButtonBlack.png"]stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
// Create button
UIButton *newButton = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 30)];
// Set button content alignment
newButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
newButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// Set button title
[newButton setTitle:@"Do Something" forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
// Set button title color
[newButton setTitleColor:[UIColor colorWithRed:255.0f/255.0 green:255.0f/255.0 blue:255.0f/255.0 alpha:1.0] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
// Add the background image
[newButton setBackgroundImage:blackButton forState:UIControlStateNormal];
// Add Events
[newButton addTarget:self action:@selector(showScanner:) forControlEvents:UIControlEventTouchUpInside];
// in case the parent view draws with a custom color or gradient, use a transparent color
[newButton setBackgroundColor:[UIColor clearColor]];
// Set titleShadowColor this way (apparently, titleLabel.shadowcolor does not work)
[newButton setTitleShadowColor:[UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:.75] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
// Set button titleLabel properties
newButton.titleLabel.font = [UIFont fontWithName:@"PTSans-Bold" size:13.0];
newButton.titleLabel.shadowOffset = CGSizeMake(1, 1);
[fullView addSubview:newButton];
}
- (void)showScanner:(id)sender
{
NSLog(@"Do something…");
}
答
你没有保持到您的视图控制器的引用。你要么需要使用
- 一个标准的iOS视图控制器经理喜欢的UITabBarController或UINavigationController的显示你的意见
- 保留您从打开它的类中的一个引用(在一个实例变量)这种视图控制器。
请注意,如果您要在ARC关闭的情况下重新编写此代码,则会出现内存泄漏,而不是崩溃。
问题是,从视图中所有对视图控制器的引用都是弱引用,这意味着它们不保留控制器*。因此,在您的加载代码中,ARC在您创建并访问其视图后释放视图控制器,并且它已经消失。
在您的应用程序中,您应该跟踪所有视图控制器,并通过它们访问其视图。像UINavigationController这样的东西为你做。
*这是因为视图控制器被认为拥有视图的所有权,并且如果视图控制器保留了视图并且视图保留了视图控制器,那么会有一个保留循环,并且它们都不会被发布。
答
我相信UIControlState
的不能&
(或|
)在一起,因为根据UIControl
Reference Docs:
控制可以有一次超过一个国家。
尝试他们分离出这样的:
// Set button title
[newButton setTitle:@"Do Something" forState:UIControlStateNormal];
[newButton setTitle:@"Do Something" forState:UIControlStateHighlighted];
[newButton setTitle:@"Do Something" forState:UIControlStateSelected];
// Set button title color
[newButton setTitleColor:[UIColor colorWithRed:255.0f/255.0 green:255.0f/255.0 blue:255.0f/255.0 alpha:1.0] forState:UIControlStateNormal];
[newButton setTitleColor:[UIColor colorWithRed:255.0f/255.0 green:255.0f/255.0 blue:255.0f/255.0 alpha:1.0] forState:UIControlStateHighlighted];
[newButton setTitleColor:[UIColor colorWithRed:255.0f/255.0 green:255.0f/255.0 blue:255.0f/255.0 alpha:1.0] forState:UIControlStateSelected];
// Add the background image
[newButton setBackgroundImage:blackButton forState:UIControlStateNormal];
// Add Events
[newButton addTarget:self action:@selector(showScanner:) forControlEvents:UIControlEventTouchUpInside];
// in case the parent view draws with a custom color or gradient, use a transparent color
[newButton setBackgroundColor:[UIColor clearColor]];
// Set titleShadowColor this way (apparently, titleLabel.shadowcolor does not work)
[newButton setTitleShadowColor:[UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:.75] forState:UIControlStateNormal];
[newButton setTitleShadowColor:[UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:.75] forState:UIControlStateHighlighted];
[newButton setTitleShadowColor:[UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:.75] forState:UIControlStateSelected];
// Set button titleLabel properties
newButton.titleLabel.font = [UIFont fontWithName:@"PTSans-Bold" size:13.0];
newButton.titleLabel.shadowOffset = CGSizeMake(1, 1);
它可能会崩溃,因为button
应该是newButton
。
更改此:
[button setTitleShadowColor:[UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:.75] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
要这样:
[newButton setTitleShadowColor:[UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:.75] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
您可能不会保留对视图控制器的引用。你在哪里加载视图控制器并显示视图? – joerick
当我点击一个按钮,并且这是我用来显示它的代码时,我从rootviewcontroller加载视图RewardsViewController * rewardsView = [[RewardsViewController alloc] initWithNibName:nil bundle:nil]; [self.view insertSubview:rewardsView.view aboveSubview:tabBar]; – morcutt
崩溃日志和堆栈跟踪请提供。 – zaph