是否有任何理由向视图添加UIPicker可能以某种方式抢占其功能的编程创建按钮?我最初实例化了一个UIButton来带我到另一个视图,一切都很好。然后我继续向相同的视图添加了一个UIPicker,目的是选择一些关键字段,然后将这些字段传递给UIButton导致的视图。不幸的是,似乎我在添加选择器的过程中以某种方式打破了我的按钮。
这是按钮的实例化:
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];
方法playGame检查选择器的字段,将它们发送到共享单例对象,然后加载游戏视图:
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];
我希望这段代码足以让某人指出我误入歧途的地方,因为我现在很迷茫。
答案 0 :(得分:0)
出于好奇,如果你更换会发生什么:
[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];
希望这可能会正确地命令您的视图,以便按钮位于顶部。另外,请查看addSubview与insertSubview的完美链接:
Difference between addSubview and insertSubview in UIView class