2010-06-24 42 views
1

將UIPicker添加到視圖中是否有任何理由可能以某種方式劫掠編程創建的按鈕的功能?我最初實例化了一個UIButton,讓我看到了不同的觀點,一切都很好。然後我繼續添加一個UIPicker到相同的視圖,目的是選擇幾個關鍵字段,然後可以傳遞到UIButton導致的視圖。不幸的是,似乎我在添加選取器的過程中以某種方式打破了我的按鈕。UIButton和UIPicker

這裏的按鈕的實例:

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]; 

方法瑣事檢查拾取器的領域中,將它們發送到一個共享singleton對象然後加載遊戲視圖這樣:

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]; 

我希望這些代碼足以讓某人指出我誤入歧途的地方,因爲此刻我已經迷失了方向。

+1

你能添加UIPicker初始化代碼?另外,點擊時按鈕是否可視化? – Endemic 2010-06-24 16:07:13

+0

什麼ViewController是按鈕的一部分?您在索引1的原始視圖頂部插入一個新的ViewController,是否佔據了整個屏幕(即使部分是透明的,就像按鈕所在的位置一樣)。看起來你可能會把一個ViewController放在你的按鈕的頂部,這可能會使按鈕失效,因爲它在你的viewController下面。 – trumpetlicks 2012-06-13 18:18:31

回答

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]; 

希望這樣可以正確排列您的視圖,以便按鈕位於頂部。也看看這裏的對比insertSubview上addSubview有很大的聯繫:

Difference between addSubview and insertSubview in UIView class