【问题标题】:iOS - UIButton event crashiOS - UIButton 事件崩溃
【发布时间】:2012-07-21 20:47:27
【问题描述】:

我在link 中尝试了 Moshe 的代码,除了“for (UIButton *button in ...”) 部分之外,它都可以正常工作,并且每次单击按钮时都会崩溃。

所以我在 viewDidLoad 方法中尝试了这段代码:

UIButton *testButton = [[UIButton alloc]initWithFrame:CGRectMake(20,50,30,30)];
    testButton.backgroundColor = [UIColor orangeColor];
    [testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [testButton setTitle:@"A" forState:UIControlStateNormal];
    [testButton addTarget:self action:@selector(commonMethodForButtons:) forControlEvents:UIControlEventTouchDown];

    [self.view addSubview:testButton];
    [testButton release];

我的项目只包含这个和 Moshe 的示例代码。知道应用程序崩溃的原因吗?我没有收到崩溃日志。

编辑:

在开放范围内我有这个方法:

-(void)commonMethodForButtons:(id)sender
{
    NSLog (@"you touched me!");
}

编辑 2:

我找到了这个问题的原因:

我在 AppDelegate 中注释掉了 [mvc release];,所以它现在可以完美运行了 :)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    MVC *mcv = [[MVC alloc]initWithNibName:nil bundle:nil];

    [self.window addSubview: mcv.view];

    //[mcv release];

    [self.window makeKeyAndVisible];

    return YES;
}

感谢您指出这一点! :)

【问题讨论】:

  • 你有实现 commonMethodForButtons: 方法吗?
  • @vladimir 我在编辑中添加了代码。这个方法在你问之前就已经存在了。
  • 尝试设置异常断点或检查控制台是否有错误消息可能是......到目前为止,您的代码看起来还不错。
  • 你现在的班级是什么?是视图控制器吗?
  • 抱歉,将“strong”替换为“retain”

标签: ios uibutton nsarray


【解决方案1】:

使用 mcv 作为属性

在AppDelegate的头文件中:

@class MVC;
    @interface AppDelegate : UIResponder {
    MVC *mcv;
}

@property (nonatomic, retain) MVC *mcv;

在实现文件中

@implementation AppDelegate

@synthesize mcv;

- (void)dealloc
{
    [mcv release];
    [super dealloc];
}

【讨论】:

  • 嗯,我在头文件中收到警告:error: expected specifier-qualifier-list before 'MVC' 我有@interface xxxAppDelegate : NSObject { ...
  • @teofile 我找到了解决方案。请看一下这个stackoverflow.com/questions/7428706/… 我收到这个警告,因为它是循环依赖。我在头文件的@interface AppDelegate ...前面添加了@class MVC;
【解决方案2】:

不,您应该通过注释掉 [mcv release] 来解决您的问题(这只是解决症状,而不是问题,如果您转换为 ARC 会导致问题,这您应该)也不应该像当前接受的答案那样将mcv(不应该是mvc?)存储为应用程序委托的ivar,但是(a)未能设置rootViewController但是(b ) 仍在使用addSubview。您应该使用应用程序委托的rootViewController。当前的解决方案不会正确配置您的视图控制器层次结构。应该是这样的:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    MVC *mvc = [[MVC alloc]initWithNibName:nil bundle:nil]; // really no NIB name?!?

    //[self.window addSubview: mvc.view];
    self.window.rootViewController = mvc;

    [mvc release];

    [self.window makeKeyAndVisible];

    return YES;
}

或者,更好的是,符合非 ARC Xcode 模板生成的标准didFinishLaunchingWithOptions,例如:

//  AppDelegate.h

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) ViewController *viewController;

@end

//  AppDelegate.m

#import "AppDelegate.h"

#import "MVC.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[MVC alloc]initWithNibName:nil bundle:nil]; // really no NIB name?!?
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

我从来不需要引用这个 viewController 属性,但鉴于这是 Xcode 生成的默认代码,我认为这是一个好习惯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    相关资源
    最近更新 更多