簡體   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