UIButton 和 UIPicker
是否有任何原因将 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];
我希望这些代码足以让有人指出我误入歧途的地方,因为我现在很迷失。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
出于好奇,如果您替换:
with
和
with
会发生什么希望这可以正确排序您的视图,以便按钮位于顶部。另请在此处查找有关 addSubview 与 insertSubview 的精彩链接:
addSubview 和 insertSubview 之间的区别在UIView类中插入Subview
Out of curiosity what happens if you replace:
with
and
with
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