1

Is there any reason adding a UIPicker to a view might somehow rob a programatically created button of its functionality? I initially instantiated a UIButton to take me to a different view, and all was well and fine. Then I went ahead and added a UIPicker to the same view with the purpose of selecting a few key fields which could then be passed on to the view the UIButton led to. Unfortunately it seems I somehow broke my button in the process of adding the picker.

Here's the instantiation of the button:

switchToGame = [UIButton buttonWithType:UIButtonTypeRoundedRect];
switchToGame.frame = CGRectMake(140,250,100,100);
[switchToGame setTitle:@"Play God" forState:UIControlStateNormal];
[switchToGame setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[switchToGame addTarget:self action:@selector(playGame) forControlEvents:UIControlEventTouchUpInside];
[self.view insertSubview:switchToGame atIndex:0];

The method playGame checks the fields of the picker, sends them to a shared singleton object then loads the game view as such:

Singleton *mySingleton = [Singleton sharedSingleton];

NSInteger numCellsRow = [theDataPicker selectedRowInComponent:0];
NSInteger aliveColorRow = [theDataPicker selectedRowInComponent:1];
NSInteger deadColorRow = [theDataPicker selectedRowInComponent:2];

UIColor * theAliveColor = [aliveColors objectAtIndex:aliveColorRow];
UIColor * theDeadColor = [deadColors objectAtIndex:deadColorRow];
NSInteger numCellsPerRow = [[cellRowOptions objectAtIndex:numCellsRow] intValue];

mySingleton.cellsPerRow = numCellsPerRow;
mySingleton.aliveColor = theAliveColor;
mySingleton.deadColor = theDeadColor;

[theAliveColor release];
[theDeadColor release];

GameController * viewController = [GameController alloc];
self.theViewController = viewController;
[self.view insertSubview:theViewController.view atIndex:1];
[viewController release];

I hope these stretches of code are enough for someone to point out where I went astray, as I am quite lost at the moment.

NWilkie
  • 156
  • 8
  • 1
    Could you add the UIPicker initialization code? Also, does the button respond visually when tapped? – Endemic Jun 24 '10 at 16:07
  • What ViewController is the button part of? You insert a new ViewController on top of the original View at index 1, does it take up the whole screen (even if parts are transparent, like where the button is). It seems like you could be putting a ViewController right on top of your button, which will in all likelyhood invalidate the button as it is underneath your viewController. – trumpetlicks Jun 13 '12 at 18:18

1 Answers1

0

Out of curiosity what happens if you replace:

[self.view insertSubview:switchToGame atIndex:0];

with

[self.view insertSubview:switchToGame atIndex:1];

and

[self.view insertSubview:theViewController.view atIndex:1];  

with

[self.view insertSubview:theViewController.view atIndex:0];

Hopefully this might order your views correctly so that the button is on top. Also look here for a great link on addSubview versus insertSubview:

Difference between addSubview and insertSubview in UIView class

Community
  • 1
  • 1
trumpetlicks
  • 7,033
  • 2
  • 19
  • 33