Reputation: 45
I started to study iOS programming with a book by Aaron Hillegass, and I have found that some samples do not work. Maybe they do not work because there is a new version of iOS and Xcode. Could you explain to me why this code is not working? The button is being created; however, it isn't receiving presses.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(10,50,100,10)];
[button setTitle:@"OK" forState:UIControlStateNormal];
self.window.backgroundColor = [UIColor whiteColor];
[self.window addSubview:button];
[self.window makeKeyAndVisible];
return YES;
}
Popular advise on such kind of question here is to add
button.userInteractionEnabled = YES;
but it doesn't help.
Sorry if my question is simple, but I spent several hours to find an answer here and on developer.apple.com.
Upvotes: 3
Views: 78
Reputation: 7553
Firstly you have the height of 10 and that's pretty small. Maybe you're not hitting it.
You can do this in the AppDelegate but you generally want to use the ViewController
. If you're new to iOS development, you certainly do not want to have any of this in the AppDelegate.
I would recommend doing all of this in a ViewController because it is overlayed on top of the UIWindow
.
Additionally, you have to add what @rahul-patel to even test if it is enabled or not.
Update: I fully agree with the other answers. All of this must/should be done. Add what @rahul-patel and @matthew-s have
Upvotes: 1
Reputation: 721
For some reason, there is a view on top of the button, which is preventing the button from receiving actions (presses). All you have to do is add:
[self.window bringSubviewToFront:button];
This line of code will put your button on top of the stack of views. Cheers
Upvotes: 2
Reputation: 1832
try adding these lines
[button addTarget:self action:@selector(hello:) forControlEvents:UIControlEventTouchUpInside];
and this is a method which will be called when button will be clicked
- (void) hello:(id) sender
{
NSLog(@"helloooo");
}
Upvotes: 1