2012-07-12 4 views
1

비슷한 질문이 게시되기 전에 게시되었지만 이제는 코드와 함께 더 명확한 질문과 자세한 정보가 있습니다. 현재 UITextField 및 UIButton있는 ViewController (SignUpViewController) 있습니다. 나는 또 다른 ViewController (ProfileViewController)에 UINavigationBar가있다. SignUpViewController의 TextField에 Username을 입력하고 UIButton을 누른 다음 ProfileViewController의 naviBar 텍스트를 SignUpViewController의 TextField에있는 텍스트로 설정하게하고 싶습니다. 문제는, ProfileViewController에서 UITextField에 액세스 할 수 없다는 것입니다. 나는 현재 "titleString"이라는 AppDelegate에 NSString을 가지고 있으며이를 솔루션의 일종으로 사용하려고합니다.다른 ViewController에서 NavigationBar 텍스트 변경

SignUpViewController :

- (IBAction)submitButton { 

    ProfileViewController *profileVC = [[ProfileViewController alloc] initWithNibName:nil bundle:nil]; 
    [self presentViewController:profileVC animated:YES completion:nil]; 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    appDelegate.titleString = @"Profile"; 

    appDelegate.titleString = usernameTextField.text; 

} 

- (void)viewDidLoad { 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

    [super viewDidLoad]; 
} 

ProfileViewController :

- (void)viewDidLoad { 

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    self.title = appDelegate.titleString; 

    [super viewDidLoad]; 
} 

모든 내 질문에 완전히 당신을 던져 경우이 스택 오버 플로우를 통해 설명하는 일종의 어렵 기 때문에 여기 내 코드는 다음과 같습니다 SignUpViewController에서 submitButton을 탭하기 전까지는 잘 작동합니다. 여기서 무슨 일이 일어나고있는거야?

답변

1

보기 컨트롤러간에 데이터를 전달하기 위해 여기에서 할 수있는 여러 가지 사항이 있습니다.

1) 위임 방법을 설정하십시오. profileViewController는 signInViewController의 델리게이트입니다. 로그인 단추를 누르면 signInViewController는 profileViewController가 수신하는 대리자 메서드를 호출하여 제목을 profileViewController에 전달합니다. signInViewController.h에서

:

@protocol SignInDelegate 

@required 
- (void)didSignInWithTitle:(NSString*)title; 

@end 

@interface SignInViewController : UIViewController 

@property (nonatomic, assign) id<SignInDelegate> delegate; 

그런 다음 ProfileViewController는 당신이 그것을 할당 할 때 대리인으로 설정됩니다 :

#import "SignInViewController.h" 

@interface ProfileViewController : UIViewController <SignInDelegate> 
:

signInViewController.delegate = profileViewController 

이 당신의 ProfileViewController.h입니다

마지막으로, ProfileViewController가 - (void) didSignInWith 제목 : (NSString *) 제목; 방법.

2) NSNotificationCenter를 사용하여 제목이 첨부 된 맞춤 알림을 게시 할 수 있습니다. 프로파일과 같이 제목을 설정하려는 다른 viewController가 여러 개있는 경우 유용합니다.

#define UPDATE_NAVBAR_TITLE @"UPDATE_NAVBAR_TITLE" 

은 signInViewController이 완료되면 :

[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(navbarUpdated) name:UPDATE_NAVBAR_TITLE object:nil]; 

을 내가 처음 추천 무엇을 요구하는지 들어 :

다음
[[NSNotificationCenter defaultCenter] postNotificationName:UPDATE_NAVBER_TITLE object:nil]; 

, 당신이 관찰자로 ProfileViewController을 추가 할 . 행운을 빕니다!

관련 문제