views:

213

answers:

4

Hey All,

Okay, so I have a viewController method that has some elements created with interface builder and some I coded myself.

I created a view in the IB...lets call it viewOne. I created a loop that creates buttons (through code) and places them on a separate view (viewTwo). This is done in a class method. However, I want the view I made to be opened everytime one of the buttons is clicked. So I did:

[button addTarget:self action:@selector(woogoo:)
             forControlEvents:UIControlEventTouchUpInside];

The woogoo class does the following:

+(void) woogoo:(id)sender { 
    //back button
    [self.view addSubview:viewOne]
}

However, the program crashes when a button is clicked. I did try making woogoo an instance method but it also crashes in this case too. All I want to do is show viewOne when a button is clicked. Any ideas what I can do?

A: 

Instead why don't you hook up an IBAction in interface builder to the button, then you can just put your [self.view addSubview:viewOne]; into that IBAction method.

PS. You're missing a semicolon after [self.view addSubview:viewOne]

Cheers

Mark Hazlett
I'm not doing it via an IBAction in the interface builder because the buttons i'm creating are constantly being created. Basically, this part of the applications is a "Notifications" section. So when a user created a notification, a new button is made and placed on the screen. I can't really call an IBAction that way.
A: 

There's not enough info but I'd guess something is wrong with the content of "viewOne" when you are in woogoo. Add this line just before the addSubview and see what you get on the console:

NSLog("woogoo: %@", viewOne);

You should see something like this, that matches the type of view and frame you created for viewOne. If you see (null) or something else well, check how viewOne gets set.

<UIView: 0x4519b70; frame = (10 1; 300 43); layer = <CALayer: 0x4519540>>

Maybe you could post more info on how/when viewOne is created/initialized.

progrmr
A: 

In +woogoo:, you invoke self.view. Since it is a class method, self is a class too, so +view must also be a class method. Did you make it a class method?

If not, you need to make -woogoo: an instance method, and also code that calls -addTarget:… an instance method, and propagate upwards.

KennyTM
A: 

Sure, sorry about that. I don't think there's anything wrong with the view. I created a view in interface builder, connected it to the view controller with the variable viewOne. If I call [self.view addSubview:viewOne] in any instance method, it works fine..not an issue whatsoever. However, I had a displayNotifications class method:

+(void) displayNotifications:(UIView*)input

This class method creates buttons dynamically based on how many "notifications" are in an array I have. It creates the buttons fine. When one of these buttons is pressed, I want it to bring up my viewOne subview. Therefore I have this set for the button:

[button addTarget:self action:@selector(woogoo:) forControlEvents:UIControlEventTouchUpInside];

woogoo is defined as:

+(void) woogoo:(id)sender {
 NSLog(@"Woo"); 
[self.view addSubview:viewOne];

}

The word "Woo" DOES display correctly so it's getting into the method fine. However, the statement after that doesn't work. I get a compiler error:

Internal compiler error: in recalculate_side_effects, at tree-gimple.c.509

I did try:

NSLog("woogoo: %@", viewOne);

..but it also crashes when it gets to this. It doesn't even give an error on the console. I think you're right saying that it can't access the view correctly. Is it even possible to run a statement like [self.view addSubview:viewOne]; in a class method like that? The buttons and class method it's trying to run are both class methods. BTW, my viewOne is declared as:

IBOutlet UIView *displayNotificationView;
@property (nonatomic, retain) IBOutlet UIView *displayNotificationView;

and it is synchronized.

Basically, all I want to do is open a damn view when the button is clicked lol I feel this should be easy. Any ideas??

Do you know what is the target-action pattern? Do you know what does the `self` means inside `+displayNotifications:`? You should re-read the *Cocoa Fundamentals Guide*.
KennyTM