UIButton and UIPicker

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.

Answers


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


Need Your Help

yii2 custom validation not working

validation yii2 yii2-validation

I need to compare 2 attribute value in the model and only if first value is lower than second value form can validate.I try with below code but it not worked.

Trying to switch URL of iframe YouTube video on click with jQuery

javascript jquery html iframe youtube

I have two divs. One contains a YouTube iframe video and the other contains a YouTube thumbnail. When the button is pressed, these are the intended steps: