繁体   English   中英

UIButton和UIPicker

[英]UIButton and 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];

我希望这些代码足以让别人指出我误入歧途,因为我现在很迷路。

出于好奇,如果您更换:

[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的链接:

UIView类中的addSubview和insertSubview之间的区别

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM