2009-09-28 8 views
1

저는 초심자 프로그래머입니다. 이것은 xcode - iPhone 용입니다. 내 아이폰 앱을 많이 만들었지 만 간단한 의사 소통 방법에 대한 이해가 부족한 것 같습니다.다른 UIViewController에서 함수 호출하기

특히 2 개의 ViewController가있는 경우.

그리고 다른 ViewController에서 ViewController의 한 함수를 호출합니다. 둘 다 tabbarController 아래에 있습니다. 내가 달성하고 싶은 것은 내가 ViewA에있을 때 tableCell을 탭한 후에 ViewB의 메소드를 호출해야하고 ViewB의 NavigationBar가 viewDetail에 푸시됩니다.

@class ViewControllerB; 
@interface SmartDDxViewController : UIViewController { 

    IBOutlet UITableView *tableView; 
    ViewControllerB *xViewController; 


} 
@property (nonatomic, retain) UITableView *tableView; 
@property (nonatomic, retain) ViewControllerB *xViewController; 

(나는 방법을 전화 드렸습니다 경우)

대 다음

ViewControllerA.h에 내가 을 사용하고 코드입니다 그리고 이것은 내가 .. ViewControllerA에 그것을 호출하는 데 사용할 것입니다. 에 ViewControllerB.m

: m Heres는

ViewControllerB *ddViewController = [[ViewControllerB alloc] init]; 
self.xViewController = ddViewController;  
[xViewController InitialiseDetailWithId:2 title:@"HEYA"]; 

InitialiseDetailWithId 코드

-(void)InitialiseDetailWithId:(NSInteger)pkey title:(NSString *)tt{ 

    NSLog(@"InitialiseDetailC========================================="); 
    AppDelegate *appDelegate = (Smart_DifferentialsAppDelegate *)[[UIApplication sharedApplication] delegate]; 
    [appDelegate GetConditionDetailsWithId:pkey]; 
    DDisViewController *viewController= [[DDisViewController alloc] initWithNibName:@"DetailView" bundle:nil]; 
    viewController.title = tt; 
    [self.NavBar pushViewController:viewController animated:YES]; 
    //[tt release]; 
    [viewController release]; 
    viewController = nil;  
    [self say:@"HEYA"]; //this is ALERTVIEW box that displays HEYA 
} 

모든 정보가 정상적으로 표시되며 alertview가 표시됩니다. 그러나 TabBar에서 View를 선택했을 때, 그 뷰는 밀리지 않았습니다.

답변

1

보기 컨트롤러 간의 직접 액세스를 사용하지 말고 대신 대리인 패턴을 사용하십시오. 이 같은 컨트롤러를 정의합니다

@protocol ViewControllerAInitDelegate; 
@interface ViewControllerA : UIViewController { 
    IBOutlet UITableView *tableView; 
    id<ViewControllerAInitDelegate> initDelegate; 
} 
@property (nonatomic, retain) UITableView *tableView; 
@property (nonatomic, assign) ViewControllerAInitDelegate *initDelegate; 
@end 

@protocol ViewControllerInitDelegate 
-(void)initializeDetailWithId:(NSInteger)pkey title:(NSString)tt; 
@end 

그래서

지금 응용 프로그램 대리자를 ViewControllerInitDelegate 프로토콜을 준수 할 수 있습니다. 그것은 다음과 비슷한 모습이 될 것입니다

@interface AppDelegate : NSObject <UIApplicationDelegate, ViewControllerInitDelegate> { 
    IBOutlet UITabBarControler* tabBarController; 
    IBOutlet ViewControllerA* controllerA; 
    IBOutlet ViewControllerB* controllerB; 
} 
@end; 

AppDelegate에이 ViewControllerAViewControllerB 모두에 대해 알고 있어야하지만보기 컨트롤러 중 어느 것도 서로에 대해 알고 있어야합니다. 이렇게하면 앱을 디버그하고 확장하는 것이 훨씬 쉬워집니다.

+0

귀하의 솔루션은 애플 리케이션을 깔끔하게 만들어 주지만 의심 할 여지없이 두 가지 이유가 있습니다. 1, ViewAController가 TabItem1에 속하고이 컨트롤러에서, 다른 DetailView에 푸시 된 ViewBController를 표시하는 tabItem2로 이동하려고합니다. . appDelegate를 통해 푸싱이 작동하는 방법을 잘 모르겠습니다. 내 솔루션에서도 작동하지 않습니다. 어쨌든 – user134611

+0

AppDelegates는 모든 탭에 대해 알고 있으므로 새 탭을 푸시하는 방법을 알고 있어야합니다. 제어 장치. '[[self.tabBarController.viewControllers objectAtIndex : 1] pushViewController : newController animated : YES]; 또는 똑같이 간단한 것이 트릭을 수행해야합니다. – PeyloW

+0

형님, 제가 말씀 드렸듯이, 저는 혼란 스러울 것이 있습니다. appDelegate에 새로운 메소드가 있어야하나요? 그 PushViewController 1 및 표시 Tab1, 그리고이 모든 내가 할 수있는 anotherViewcontrollerB, 아마도 [appdelegate NewMethodinAppDel : sd :]; 그런 식으로 ?? – user134611

0

각보기 컨트롤러에서 인스턴스 변수/속성을 추가하여 다른보기 컨트롤러를 추적 할 수 있습니다. 당신은 예를 들어있을 수 있습니다

:

이뿐만 아니라 쉽게 다른 뷰 컨트롤러에서 메서드를 호출하고 공용 속성에 액세스 할 수 있습니다, 그러나 그것은 또한 당신에게 둘 사이에 뒤집기의 쉬운 방법을 허용
@interface ThisViewController : UIViewController { 
    SomeViewController  *sViewController; 
// other instance variables 
} 
@property (nonatomic, retain) SomeViewController  *sViewController; 

(애니메이션 유무에 관계없이).

+0

답장을 보내 주신 분께 감사드립니다. 문제는 해결되었지만 새로운 문제가 생겼습니다. 다시 질문을 읽어주세요!, myBad! – user134611

+0

보기 컨트롤러에 직접 액세스하는 것은 대개의 경우 나쁜 생각입니다. 메모리 누출로 이어질 수있는 순환 참조가있는 잘못된 디자인입니다. – PeyloW

1
//current view controller index 
int currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController]; 

//previous view controller (index -1) 
AccountViewController *account = (AccountViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex - 1]; 

(access to anything you want) 
account.property = object; 
[account doSmthng]; 
관련 문제