1

ビューに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];

メソッド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];

私は今のところかなり迷っているので、これらのコードの範囲が、私がどこを迷ったのかを誰かが指摘するのに十分であることを願っています。

4

1 に答える 1

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

ボタンが一番上になるように、これでビューが正しく並べ替えられることを願っています。また、addSubview と insertSubview に関する優れたリンクについては、こちらを参照してください。

UIView クラスの addSubview と insertSubview の違い

于 2012-06-13T18:21:09.290 に答える