每当我点击“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…");
}
您可能不会保留对视图控制器的引用。你在哪里加载视图控制器并显示视图? – joerick
当我点击一个按钮,并且这是我用来显示它的代码时,我从rootviewcontroller加载视图RewardsViewController * rewardsView = [[RewardsViewController alloc] initWithNibName:nil bundle:nil]; [self.view insertSubview:rewardsView.view aboveSubview:tabBar]; – morcutt
崩溃日志和堆栈跟踪请提供。 – zaph