0

나는 UIViewController이고 UITableViewController입니다.
ViewController에서 UIButtonUITextfield이 있는데, 버튼을 누르면(iPhone 5S, iPhone 6, iPhone 6S, iPhone 7)과 같은 tableview의 정적 데이터로 이동합니다. ViewController의 텍스트 필드에 표시됩니다. 시도했지만 이것을 얻을 수 없습니다. 이 문제를 해결하도록 도와주세요. 아래에있는 내 코드입니다 :테이블 뷰에서 뷰 컨트롤러 텍스트 필드로 데이터를 다시 전달 목표 - C

ViewController.h 
———————————————— 
#import <UIKit/UIKit.h> 
#import "Utility.h" 

@interface ViewController : UIViewController 
- (IBAction)oneButtonClicked:(id)sender; 
@property (strong, nonatomic) IBOutlet UITextField *txtOne; 


ViewController.m 
———————————————— 

//Can't understand what to do in this viewcontroller. 

TableViewController.h 
—————————————————————- 
#import <UIKit/UIKit.h> 
#import "Utility.h" 
@interface FamilyDetailsViewController : UITableViewController 
@end 


TableViewController.m 
—————————————————————- 

#import "FamilyDetailsViewController.h" 
@interface FamilyDetailsViewController() 
{ 
    NSArray *arr; 
} 
@end 

@implementation FamilyDetailsViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    arr=[[NSArray alloc]initWithObjects:@"Parent",@"Grandparent",@"Sibling",@"Child",@"Other", nil]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

    return [arr count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text=[arr objectAtIndex:indexPath.row]; 


    return cell; 
} 


#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    [self.navigationController popViewControllerAnimated:YES]; 
} 
+0

는 참조 파일 ViewControllerB.m에서 http://stackoverflow.com/questions/19343519/pass- data-back-to-previous-viewcontroller – suhit

+1

충분히 검색 한 후 질문을 게시하십시오. 귀하의 문제에는 수천 가지 해결책이 있습니다. [This] (http://stackoverflow.com/questions/626898/how-do-i-create-delegates-in-objective-c) 도움이 될 것입니다. – iPeter

+0

맞춤형 프로토콜은 피터가 링크와 suhit의 대답을 제공 할 때 viewcontrollers간에 데이터를 전달하는 가장 좋은 방법입니다. –

답변

0

이를 따라

@property (strong, nonatomic) NSString *selectedText; 

ViewController.m에서 AppDelegate.h에서

-(void)viewWillAppear:(BOOL)animated 
{ 
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; 

    txtOne.text = appDelegate.selectedText; 

} 

appDelegate.selectedText = @"" 동안 확인 UITableViewController 클래스로 이동합니다. FamilyDetailsViewController.m에서

프라그 마크 -는 싱글 클래스의 설정 값을 저장하는 이러한 방식으로 테이블 뷰 대리인

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

     NSString *selected= [arr objectAtIndex:indexPath.row]; 

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

     appDelegate.selectedText = selected; 

     [self.navigationController popViewControllerAnimated:YES]; 
} 

(APP 대리자).. ViewController (viewWillAppear)에서이 데이터를 가져올 수 있습니다.

+0

또한 이전 ViewController에서 데이터를 채우는 몇 ​​가지 방법이 있습니다. 블록, 대리인, NSNotificationCenter. iOS 개발에 익숙하지 않다고하셨습니다. 그래서 싱글 톤 클래스를 사용하는 가장 간단한 방법을 말씀 드렸습니다. – SNarula

1

NSNotification을 사용해보세요. NSNotification으로 데이터를 ViewController으로 다시 전달하십시오. 먼저 viewWillAppearViewController의 알림을 등록하십시오.

ViewController에서 :

-(void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:YES]; 
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getTextFromTable:) name:@"PassTextBackToViewController" object:nil]; 
} 

다음과 같이 ViewController의 메소드를 정의

-(void)getTextFromTable:(NSNotification *)notification 
{ 

    [self.textField setText:[notification.userInfo valueForKey:@"cellText"]]; 
} 

그리고 tableView'sdidSelectRowAtIndexPath 방법, 포스트 알림 :

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath]; 
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:cell.textLabel.text,@"cellText", nil] 
    [[NSNotificationCenter defaultCenter]postNotificationName:@"PassTextBackToViewController" object:dictionary]; 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
을 코드 아래에보십시오

물론이 알림의 이름이 모두의 ViewController에서 동일해야합니다 확인하고 TableViewController

1
in `ViewControllerB.h` which is a TableViewController declare a delegate to pass the data to ViewControllerA and create a delegate object 

#import <UIKit/UIKit.h> 

@protocol DataDelegate <NSObject> 

- (void)passData:(NSString *)data; 

@end 

@interface ViewControllerB : UIViewController 

@property (weak, nonatomic) id<DataDelegate> delegate; 

@end 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

     NSString *selectedText = titleArray[indexPath.row]; 

     if [self.delegate respondsToSelector:@selector(passData:)]) { 
      [self.delegate passData:"hello"]; 
     } 

     [self.navigationController popViewControllerAnimated:YES]; 
} 



**in ViewControllerA** 

#import "ViewControllerA.h" 
#import "ViewControllerB.h" 

@interface ViewControllerA() <DataDelegate> 
@property (strong, nonatomic) ViewControllerB *viewControllerB; 
@end 

@implementation ViewControllerA 

- (void)viewDidLoad { 
    _viewControllerB.delegate = self; 
} 

- (void)passData:(NSString *)data { 
    self.textField.text = data 
}