UIButton 和 UIPicker

发布于 2024-09-06 23:35:08 字数 1600 浏览 4 评论 0原文

是否有任何原因将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2024-09-13 23:35:09

出于好奇,如果您替换:

[self.view insertSubview:switchToGame atIndex:0];

with

[self.view insertSubview:switchToGame atIndex:1];

[self.view insertSubview:theViewController.view atIndex:1];  

with

[self.view insertSubview:theViewController.view atIndex:0];

会发生什么希望这可以正确排序您的视图,以便按钮位于顶部。另请在此处查找有关 addSubview 与 insertSubview 的精彩链接:

addSubview 和 insertSubview 之间的区别在UIView类中插入Subview

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文