3

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.

Matthew S.
  • 711
  • 1
  • 5
  • 22
gurk
  • 45
  • 5

3 Answers3

2

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

Matthew S.
  • 711
  • 1
  • 5
  • 22
  • Thanks much, it helped! – gurk Jan 30 '16 at 17:24
  • Awesome, if this answered your question (or any other answer did), please click the check. It will help others find the correct answer. If this helped your question (or any other answer did), please vote on the question. – Matthew S. Jan 30 '16 at 17:30
1

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");
}
Rahul Patel
  • 1,822
  • 12
  • 21
1

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

iSkore
  • 7,394
  • 3
  • 34
  • 59
  • 1
    Problem solved, but thank you for your advise, I will read about it! – gurk Jan 30 '16 at 17:26
  • Yup, looked like @matthew-s had the solution, if this helped, check up, if not, check down or neither. So are you looking to have a button that always overlays the UIViewControllers? – iSkore Jan 30 '16 at 17:45
  • Checked up Mathew's answer, thanks. I'm new here, so just learning the rules :) – gurk Jan 30 '16 at 17:48
  • I'm just trying to verify my understanding of guides on developer.apple.com, so my code is just a sample, not a part of real app. – gurk Jan 30 '16 at 17:50
  • Yup no problem, if the answer helped you, even if it's not the exact solution, you can vote up if it contains good info. If it's the exact solution, you're gonna want to check it as correct (I see you did that on the correct answer), you can also up vote the correct answer. If the answer was incorrect, down vote it so others don't attempt doing it. – iSkore Jan 30 '16 at 17:50
  • Ok, I got it, but can't vote yet - need a bit more reputation points – gurk Jan 30 '16 at 17:52
  • Thanks for your help, it was really nice to get it so fast. – gurk Jan 30 '16 at 17:54
  • Ahh gotcha. Yup that's fine, you kinda only want to mess with the AppDelegate if you're doing more advanced things. I would highly recommend checking out http://www.raywenderlich.com/ Ray has much better, easier to understand tutorials for everything. He does a very good job of walking you through each step AND telling you WHY it's like that, not just here paste this in. He also has consistent format and expansive database of lessons – iSkore Jan 30 '16 at 17:54
  • You got it! How much do you need? Like 25 or so? – iSkore Jan 30 '16 at 17:55
  • Haha hey there ya go, that was quick – iSkore Jan 30 '16 at 18:10
  • Thanks, now I'm fine ;) – gurk Jan 30 '16 at 18:19