当我点击它时,为什么这个按钮会崩溃我的应用程序? UIButton iPhone(ARC)

时间:2011-11-26 19:58:21

标签: iphone

每当我点击" newButton"我的应用程序崩溃了。我正在使用自动引用计数。

编辑:只是在另一个应用程序中尝试了这个并且它可以工作,但不能用我自己的工作。

这是我的代码:

- (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…");
}

enter image description here

2 个答案:

答案 0 :(得分:3)

我认为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];

答案 1 :(得分:0)

您没有保留对视图控制器的引用。你需要

  • 使用标准的iOS视图控制器管理器(如UITabBarController或UINavigationController)来显示您的视图
  • 在您打开它的类中为此视图控制器保留一个引用(在实例变量中)。

请注意,如果您在关闭ARC的情况下重新编写此代码,则会出现内存泄漏,而不是崩溃。

问题是视图中对视图控制器的所有引用都是引用,这意味着它们不会保留控制器*。因此,在您的加载代码中,ARC在您创建并访问其视图后释放视图控制器,它已经消失。

在您的应用中,您应该跟踪所有视图控制器,并通过它们访问它们的视图。像UINavigationController这样的东西可以帮到你。

*这是因为视图控制器被认为对视图具有所有权,并且如果视图控制器保留视图并且视图保留了视图控制器,则会有一个保留循环,并且它们都不会是释放。