I am a total beginner to Objective C.
Currently I try to display a view in my main window. This view contains a button. For some reason by clicking this button xcode throws an error.
Hopefully you can help me to understand what I am doing wrong.
- (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];
BtnView *btn = [[BtnView alloc] init];
[self.window addSubview:btn.view];
[self.window makeKeyAndVisible];
return YES;
}
The viewController of my button view has an action for recognizing a touch up event
h file:
- (IBAction)touched:(id)sender;
and in the m file
- (IBAction)touched:(id)sender {
//actions ...
}
By touching the button I get the following error:

Whats wrong?
m file:

h file:

I assume you have ARC enabled for this project.
When you return from
didFinishLaunchingWithOptions:the object assigned tobtnwill be released since you assign it to a local variable that is only defined within that method. MakeBtnView *btnastrongproperty of your class and it should stay around to handle the button presses. (Remember to useself.btnin references to it.)(But, I’m curious, why not just put the button on your main view in the first place and then not worry about an extra controller and subviews?)