私は、いくつかのセクションで構成される大規模なプロジェクトに取り組んでおり、それぞれにさまざまな数のスライドがあります。
ボタンの数が読み込まれたセクションのスライドに対応するナビゲーション バーを動的に作成しようとしています。私が抱えている問題は、各スライドが読み込まれるときにボタンのオン/オフの外観を設定することです。
セクションの作成時にナビゲーション バーを作成し、各スライドが読み込まれるたびに各ボタンの外観を更新する必要があります。を使用して各ボタンをターゲットにしようとしていますviewWithTag
。セクションの最初のスライドがロードされたときの外観は問題ありませんが、次のボタンを押すとすべてが消えます。不思議なことに、クリックして最初のスライドに戻ると、すべてが正常に表示されます。
セクションが読み込まれると、必要なスライド情報を含むディクショナリ オブジェクトの変更可能な配列が作成されます。メイン ビューから呼び出されるクラス メソッド ( - (void) newbutton:(NSString *) title withIndex:(int) index atPosition:(int) Xpos
) があります。これは、plist ファイルからタイトルとインデックスの引数を渡し、for ループを介してカスタムの "ON" および "OFF" ボタンを作成します。その後、インデックス 0 のスライドが読み込まれます。
メインビュー
- (void) findStory:(NSString *) presentation
{
[model loadStory:presentation];
int storyCount = model.currentStory.count;
if (storyCount > 1) {
int distribution = 700 / (storyCount - 1); // 700 = width of Navigation Rule
int Xpos = 89; // 89 = X position of Navigation Rule
// Build Navidation trail
for (int i = 0; i < model.currentStory.count; i++) {
NSLog(@"findStory(): Item Title: %@", [model.currentStory[i] objectForKey:@"name"]);
[nav newbutton:[model.currentStory[i] objectForKey:@"name"] withIndex:i atPosition:Xpos];
navButton = nav.button;
navDot = nav.buttonDot;
[self.navHolder addSubview:navDot];
[self.navHolder addSubview:navButton];
Xpos += distribution;
}
}
[self newPage:model.pageIndex];
}
- (void)newPage:(int) index
{
NSLog(@"newPage(): Requested page Index is %i", model.pageIndex);
// Code that finds and loads the slide
// At the end the navigation appearance is set
[self setNavAppearance:index];
}
- (void) setNavAppearance:(int) index
{
NSLog(@"setNavAppearance(): Setting navigation appearance...");
int storyCount = model.currentStory.count;
if (storyCount > 1) {
for (int i = 0; i < storyCount; i++) {
int pressedIndex = i + 20;
int viewTag = i;
UIButton *onButton = (UIButton *)[self.navHolder viewWithTag:viewTag];
if (i != index) {
onButton.hidden = YES;
} else {
onButton.hidden = NO;
}
UIButton *offButton = (UIButton *)[self.navHolder viewWithTag:pressedIndex];
offButton.hidden = NO;
}
}
NSLog(@"setNavAppearance(): navigation appearance set.");
}
nav クラス メソッド
- (void) newbutton:(NSString *) title withIndex:(int) index atPosition:(int) Xpos
{
buttonIndex = index;
button = [[UIButton alloc] init];
int labelXPosition = (Xpos - 75);
int dotXposition = (Xpos - 10);
// ON Button
buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(labelXPosition, 637, 150, 55)];
buttonLabel.textColor = [UIColor blackColor];
buttonLabel.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:12];
buttonLabel.textAlignment = NSTextAlignmentCenter;
buttonLabel.lineBreakMode = NSLineBreakByWordWrapping;
buttonLabel.numberOfLines = 2;
buttonLabel.text = title;
buttonLabel.backgroundColor = [UIColor clearColor];
buttonLabel.textColor = [UIColor colorWithRed:0.047f green:0.337f blue:0.494f alpha:1.0f];
[self.button addSubview:buttonLabel];
dot = [[UIImageView alloc] initWithFrame:CGRectMake(dotXposition, 702, 20, 20)];
[dot setImage:[UIImage imageNamed:@"buttonPressed.png"]];
[self.button addSubview:dot];
button.tag = buttonIndex;
// OFF Button
buttonDot = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonDot.frame = CGRectMake(dotXposition, 702, 20, 20);
buttonDot.backgroundColor = [UIColor clearColor];
UIImage *dotImage = [UIImage imageNamed:@"button.png"];
[buttonDot setBackgroundImage:dotImage forState:UIControlStateNormal];
UIImage *dotImagePress = [UIImage imageNamed:@"buttonPressed"];
[buttonDot setBackgroundImage:dotImagePress forState:UIControlStateHighlighted];
int dotIndex = buttonIndex + 20;
buttonDot.tag = dotIndex;
}
何が起こっているのかわかりません。この-(void) setNavAppearance
メソッドは、新しいページが読み込まれるたびに実行され、理論的には目的の外観を設定する必要があります。
誰かが提供できる助けをいただければ幸いです。
ありがとう