0

我在此链接中尝试了 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;
}

感谢您指出这一点!:)

4

2 回答 2

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];
}
于 2012-07-22T13:08:41.090 回答
0

不,您应该通过注释掉来解决您的问题[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;
}

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

//  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;
}

I've never had to refer to this viewController property, but given that this is the default code that Xcode generates, I assume it is good practice.

于 2012-07-24T15:28:51.797 回答