1

Okay, so this is pretty weird; before upgrading to the final GMseed of Xcode my buttons worked perfectly fine... Now I can't even do them on a super simple scale without the program crashing on click.

The absolute most bizarre aspect is that it works SOME of the time, but like 85%, it just crashes; and when it does work, it'll crash after extensive use.

Here is my code (stripped down to the simplest button implementation I could think of):

@interface TESTViewController ()
{
    UIButton *button;
}

@end

@implementation TESTViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addButton];
    [button addTarget:self action:@selector(buttonPressed) 
    forControlEvents:UIControlEventTouchDown];
}

- (void)addButton
{
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];

    [button setTitle:@"CLICK" forState:UIControlStateNormal];

    [button setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:button];
}

- (void)buttonPressed
{
    NSLog(@"WORKED");
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];


    TESTViewController *test = [[TESTViewController alloc] init];

    [self.window addSubview:test.view];

    return YES;
}

I've tried creating the button in the init function, viewDidLoad -- neither works consistently, though viewDidLoad seems to work a little more frequently...

I tried -(void) addButton:(UIButton*)button as well Using:

[button addTarget:self action:@selector(buttonPressed:) 
        forControlEvents:UIControlEventTouchDown]; 

--No difference.

I am at a total loss. I tried looking it up here on the forums, but unfortunately most of it refers to programming with IBActions.

The error I get is bad access So my guess is that when the button is clicked, it's not actually accessing the buttonPressed function but something else entirely... I have no clue as to why this would be the case...

Thanks for any help!

karthika
  • 4,085
  • 3
  • 21
  • 23
Nolan Anderson
  • 576
  • 1
  • 6
  • 15

5 Answers5

3

The problem is in your "didFinishLaunchingWithOptions" method. Here you are creating a local object which gets Deallocated i.e "TESTViewController *test = [[TESTViewController alloc] init]". You should retain this object, so try making it a property and use "self.test = [[TESTViewController alloc] init]". It will work. Also conventionally you should use rootViewController rather adding your viewController to windows's subview.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.test = [[TESTViewController alloc] initWithNibName:nil bundle:nil];
    self.window.rootViewController = self.test;
    [self.window makeKeyAndVisible];
    return YES;
}
RoHaN
  • 372
  • 3
  • 14
  • Also to get which instance is deallocated you can use "Enable Zombie Objects" from "Diagnostics" tab in "Edit Schemes". This [link](http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode) might help. – RoHaN Sep 25 '13 at 05:22
1

I think you have a memory leak on that Button object are you using ARC or manual memory management? If manual make sure you retain it. If it is ARC use a property and the compiler will do the rest of the work

@property (nonatomic, strong) UIButton *button;
PaulWoodIII
  • 2,636
  • 7
  • 31
  • 35
0

try it this way

- (void)addButton
{
    button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
    [button addTarget:self action:@selector(buttonPressed) 
                 forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"CLICK" forState:UIControlStateNormal];

    [button setBackgroundColor:[UIColor blackColor]];

    [self.view addSubview:button];
}

and remove

[button addTarget:self action:@selector(buttonPressed) 
forControlEvents:UIControlEventTouchDown];

this line from viewDidLoad

D-eptdeveloper
  • 2,430
  • 1
  • 16
  • 30
0

I am not sure if this is the cause of the crash but actions (like your buttonPressed) should have a sender parameter:

[button addTarget:self action:@selector(buttonPressed:) 

- (void)buttonPressed:(id)sender
{
...
}
diederikh
  • 25,221
  • 5
  • 36
  • 49
-1

To define a button programatically use the buttonWithType method, Try this

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(buttonPressed)
 forControlEvents: UIControlEventTouchUpInside];//Any control event type
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 80, 45);
[view addSubview:button];
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101