2014-09-18 13 views
0

두 개의 viewController가 있습니다. 그 중 하나에는 UILabel과 UIButton이 있고 다른 하나에는 UITableView가 있습니다. 첫 번째보기의 UIButton을 누르면 새 ViewController가 표시되고 tableview의 행이 선택되면 tableview가 닫히고 첫 번째 viewController의 UILabel이 업데이트되어 텍스트가 tableview. 코드 :UILabel이보기에 추가되지 않았습니다.

최초의 ViewController에서 :

- (void)changeTitleWithString:(NSString *)title 
{ 
    UILabel* selected_station = [[UILabel alloc ]initWithFrame:CGRectMake(45, 153+45, 231, 20)]; 
    [selected_station setFont:[UIFont systemFontOfSize:16.0]]; 
    selected_station.textAlignment = NSTextAlignmentCenter; 
    selected_station.text = [NSString stringWithFormat:@"%@", title]; 
    [self.view addSubview:selected_station]; 
    NSLog(@"%@", selected_station); 
    NSLog(@"%@", title); 
} 

초의 ViewController에서

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.currentString = [NSString stringWithFormat:@"Cell %d", indexPath.row+1]; 

    NewViewController *viewCOntroller = [[NewViewController alloc] init]; 
    [viewController changeTitleWithString:self.currentString]; 

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];  
} 

changeTitleWithString 함수가 호출되고있는 tableview에서 문자열이 성공적으로 기능 수신되지만 UILabel의 원 나타나지 마. UILabel (selected_station)을 로그 아웃 할 때 그것은 0이 아니며 텍스트가 정확합니다. 보기에 문자열이 나타나지 않는 이유는 무엇입니까?

+0

도움이되기를 바랍니다 두 번째보기 컨트롤러. – Kaps18

+1

새로운 'NewViewController'인스턴스를 만들었지 만 표시하지 않는 것은 기본적으로 아무 것도하지 않는다는 것을 의미합니다. 두 번째보기 컨트롤러로 전달되는 'changeTitleWithString'메서드 또는 이와 유사한 방법에 대한 참조 인 첫 번째보기 컨트롤러에 대한 참조가 필요합니다. – trydis

+0

이 댓글을 답으로 써야합니다 @trydis –

답변

1

이전 할당 된보기 컨트롤러 (NewViewController)를 다시 참조하는 것처럼 보이지 않습니다. 오히려 새로운 NewViewController를 할당하려고합니다.

그래서 대리인 속성이 두 번째 뷰 컨트롤러를 만들 -

더 좋은 방법은 두 번째 뷰 컨트롤러에 대한 위임 프로토콜을 만들고 그것의 위임 NewViewController을 만들 수 있습니다. SecondViewController의 구현에 지금이

@protocol SecondViewControllerDelegate; 

@interface SecondViewController : UIViewController 

@property (nonatomic, strong) NSString *currentString; 
@property (nonatomic, assign) id<SecondViewControllerDelegate>delegate; 
@end 

//---------------------------------------------------------------------------------- 
@protocol SecondViewControllerDelegate <NSObject> 

@optional 
-(void) secondViewController:(SecondViewController*)secondViewController didChangeSelectionText:(NSString *)string; 
@end 

같은

뭔가가 FirstViewController (당신의 NewViewController)의 구현 지금이

@implementation SecondViewController 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.currentString = [NSString stringWithFormat:@"Cell %ld", indexPath.row+1]; 

    if ([self.delegate respondsToSelector:@selector(secondViewController:didChangeSelectionText:)]) { 
     [self.delegate secondViewController:self didChangeSelectionText:self.currentString]; 
    } 
    [self.navigationController dismissViewControllerAnimated:YES completion:nil]; 
} 

@end 

그런 짓을이

그런 짓을
@implementation FirstViewController 

-(void) showSecondController 
{ 
    SecondViewController *viewController = [[SecondViewController alloc] init]; 
    [viewController setDelegate:self]; // set this controller as the delegate 
    // add the rest of the display code here 
} 

#pragma mark - SecondViewControllerDelegate Methods 
-(void) secondViewController:(SecondViewController*)secondViewController didChangeSelectionText:(NSString *)string 
{ 
    //change your text here 
} 
@end 

이것은 당신에게 pointe를 줄만큼 충분해야합니다. 올바른 방향으로 D

FirstViewController에서
+0

답변 해 주셔서 감사합니다! 임 비록 당신을 이해하는지 확실하지 않습니다. 메서드 선언은 두 번째 viewController 또는 NewViewController에서 .h- 파일로 이동해야합니까? 그리고 메소드 구현은 어디로 가야합니까? –

+0

대표자 아이디어와 같은 소리가 약간 혼란 스럽습니다. https://developer.apple.com/library/ios/documentation/cocoa/conceptual/programmingwithobjectivec/ProtocolswithWorkingwithProtocols와 함께 살펴보십시오.html – pds

+0

더 완벽한 코드 예제로 답변을 업데이트했습니다. – pds

1

- (void)changeTitleWithString:(NSString *)title :(UIView *)labelView 
{ 
    UILabel* selected_station = [[UILabel alloc ]initWithFrame:CGRectMake(45, 153+45, 231, 20)]; 
    [selected_station setFont:[UIFont systemFontOfSize:16.0]]; 
    selected_station.textAlignment = NSTextAlignmentCenter; 
    selected_station.text = title; 
    [labelView addSubview:selected_station]; 
    NSLog(@"%@", selected_station); 
    NSLog(@"%@", title); 
} 

InSecondViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    self.currentString = [NSString stringWithFormat:@"Cell %d", indexPath.row+1]; 

    NewViewController *viewCOntroller = [[NewViewController alloc] init]; 
    [viewController changeTitleWithString:self.currentString:self.view]; 

    [self.navigationController dismissViewControllerAnimated:YES completion:nil];  
} 

나는 그것이 프리스트 뷰 컨트롤러의 선물에있는 버튼을 클릭하는 방법, 당신은 전체 코드를 공유 할 수 있습니다하십시오

관련 문제