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.
Out of curiosity what happens if you replace:
[self.view insertSubview:switchToGame atIndex:0];
[self.view insertSubview:switchToGame atIndex:1];
[self.view insertSubview:theViewController.view atIndex:1];
[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: