0

I tried the codes of Moshe in this link, and it worked except the part of "for (UIButton *button in ..." and it crashes every time when I click on a button.

So I tried this code in the viewDidLoad method:

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];

My project contains nothing than this and Moshe´s sample codes. Any idea why the app crashes? I get no crash log.

EDIT:

in the open scope I have this method:

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

EDIT 2:

I found the reason to this problem:

I commented out [mvc release]; in AppDelegate, so it works perfectly now :)

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

Thank you for pointing this out! :)

Community
  • 1
  • 1
wagashi
  • 894
  • 3
  • 15
  • 39

2 Answers2

1

Use mcv as property

In the header file of the AppDelegate:

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

@property (nonatomic, retain) MVC *mcv;

In the implementation file

@implementation AppDelegate

@synthesize mcv;

- (void)dealloc
{
    [mcv release];
    [super dealloc];
}
wagashi
  • 894
  • 3
  • 15
  • 39
  • Um, I get warning in the header file: `error: expected specifier-qualifier-list before 'MVC'` I have @interface xxxAppDelegate : NSObject { ... – wagashi Jul 22 '12 at 13:34
  • 1
    @teofile I found the solution. Please take a look at this http://stackoverflow.com/questions/7428706/error-expected-specifier-qualifier-list-before-xxx I get this warning because it is cyclical dependencies. I added `@class MVC;` in front of `@interface AppDelegate ...` in the header file. – wagashi Jul 23 '12 at 11:23
0

No, you should not be solving your problem by commenting out the [mcv release] (which is only fixing a symptom, not the problem, and will cause problems if you ever convert to ARC, which you should) nor should you, as in the currently accepted answer, be storing mcv (shouldn't that be mvc?) as an ivar of the app delegate but (a) failing to set rootViewController but (b) still using addSubview. You should use the app delegate's rootViewController. The current solutions will not configure your view controller hierarchy correctly. It should be as follows:

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

Or, better, conform to the standard didFinishLaunchingWithOptions generated by the non-ARC Xcode templates, e.g.:

//  AppDelegate.h

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

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

@end

And

//  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.

Rob
  • 415,655
  • 72
  • 787
  • 1,044