모두 동일한 선택기 메서드를 공유하는 프로그래밍 방식으로 생성 된 UIButton이 있습니다. 메서드가 실행될 때 어떤 버튼이 눌 렸는지 확인한 다음 해당 UIViewController를로드 할 수 있기를 바랍니다.
-(void)buildButtons
{
for( int i = 0; i < 5; i++ ) {
UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTag:i];
[aButton addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
[aView addSubview:aButton];
}
그때:
- (void)buttonClicked:(UIButton*)button
{
NSLog(@"Button %ld clicked.", (long int)[button tag]);
// code here that picks the correct viewController to push to...
// for example tag 1 would create an instance of vcTwo.m and would then be pushed to the navigationController and be displayed on screen
}
세 가지 UIViewController 클래스 (vcOne.m, vcTwo.m, vcThree.m)가 있고 버튼을 눌렀을 때 'buttonClicked'가 실행되고 코드가 푸시 할 해당 viewController를 선택하도록 원합니다. 결국 수십 / 수백 개의 viewController가있을 수 있으므로 일련의 if 문을 사용하고 싶지 않습니다. 모든 viewController를 인스턴스화하고 배열에 넣어야합니까? 더 좋은 방법이 있습니까?
결국 나는 이것과 함께 갔다.
- (void) buttonClicked:(id)sender
{
NSLog(@"Button tag = %li", (long)[sender tag]);
FormularyVC *formularyVCInstance = [FormularyVC alloc];
ProceduresVC *proceduresVCInstance = [ProceduresVC alloc];
VetMedVC *vetMedVCInstance = [VetMedVC alloc];
NSArray *vcArray = [NSArray arrayWithObjects:formularyVCInstance, proceduresVCInstance, vetMedVCInstance, nil];
UIViewController *vcToLoad = [vcArray objectAtIndex:(int)[sender tag]];
vcToLoad.view.backgroundColor = [UIColor whiteColor];
[self.navigationController pushViewController:vcToLoad animated:NO];
}
어떤 버튼을 눌렀는지에 따라로드 할 수 있기를 원하는 ViewController의 배열을 만들었습니다. 버튼을 누르면 메소드가 실행되고 태그가 매개 변수로 사용됩니다. 이 태그는 배열의 인덱스에서 위치를 확인하여 필요한 ViewController를 찾는 데 사용됩니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다