10

Tôi có đại biểu làm việc khi dữ liệu đang được chuyển từ phương thức sang trình điều khiển xem trình bày. Nhưng bộ điều khiển xem trình bày không hiển thị dữ liệu mà nó nhận được từ phương thức. Tôi nhìn vào các bài viết khác và họ nói để sử dụng phương thức đại biểu/giao thức, nhưng không giải thích làm thế nào/tại sao trình bày VC làm mới. Tôi giả định rằng đại biểu của tôi được thiết lập không chính xác. Nếu không, phương pháp làm mới dữ liệu là gì? Tôi đã kiểm tra và viewWillAppear và viewDidAppear không được gọi.Làm mới dữ liệu trong UIViewController sau khi loại bỏ bộ điều khiển chế độ xem được trình bày của nó qua đại biểu

SCCustomerDetailVC.h (Trình bày VC)

#import "SCCustomersVC.h" 

@interface SCCustomerDetailVC : UIViewController <SCCustomersVCDelegate> 

@property (atomic, strong) SCCustomer *customer; 
@property (strong, nonatomic) IBOutlet UIButton *changeCustomerButton; 

- (IBAction)changeCustomerButtonPress:(UIButton *)sender; 

@end 

SCCustomerDetailVC.m (Trình bày VC)

- (IBAction)changeCustomerButtonPress:(UIButton *)sender 
{  
    UINavigationController *customersNC = [self.storyboard instantiateViewControllerWithIdentifier:@"customersNC"]; 
    SCCustomersVC *customersVC = (SCCustomersVC *)customersNC.topViewController; 
    customersVC.delegate = self; 
    [self presentViewController:customersNC animated:YES completion:nil]; 
} 

//Protocol methods 
- (void)passCustomer:(SCCustomer *)customer 
{ 
    self.customer = customer; 

    //At this point, self.customer has the correct reference 

    [self dismissViewControllerAnimated:YES completion:nil]; 
} 

SCCustomersVC.h (Modal VC)

#import "SCCustomersVCDelegate.h" 

@class SCCustomerDetailVC; 

@interface SCCustomersVC : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate> 

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

@end 

SCCustomersVC.m (Modal VC)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    SCCustomer *customer = [self customerAtIndexPath:indexPath]; 
    [self.delegate passCustomer:customer]; 
} 

SCCustomersVCDelegate.h

@class SCCustomer; 

@protocol SCCustomersVCDelegate <NSObject> 
@optional 

- (void)passCustomer:(SCCustomer *)customer; 

@end 
+0

bạn đã làm gì sau khi nhận được tham chiếu chính xác trong đại biểu của bạn. – Zen

+0

@Zen Yup, tôi xin lỗi vì đã làm phiền tất cả các bạn vì điều này. Vì lý do nào đó, tôi nghĩ rằng nó sẽ "kỳ diệu" cập nhật vào đêm cuối cùng của chính mình trong trạng thái mệt mỏi của tôi. Tôi cần phải thêm mã để cập nhật nó. – guptron

Trả lời

6

Tôi nghĩ bạn gần đó. EDIT - chỉ học được here rằng hành vi viewWillAppear khác trong iOS> 5. Bạn vẫn muốn xem sẽ xuất hiện để cập nhật xem của bạn với trạng thái mô hình của bạn, vì nó cần phải làm điều đó trên bản trình bày ban đầu.

Và bạn có thể gọi nó từ vc phương thức của bạn hoặc từ bên trong phương thức ủy quyền. Vì vậy, thêm mã để viewWillAppear rằng cập nhật xem với tình trạng mô hình bộ điều khiển quan điểm của ...

// SCCustomerDetailVC.m 
- (void)viewWillAppear:(BOOL)animated { 
    [super viewWillAppear:animated]; 

    // making up an IBOutlet called someLabel 
    // making up a model method (description) that returns a string representing your model 
    self.someLabel.text = [self.customer description]; 
} 

Sau đó một trong hai từ vc trình bày hoặc các đại biểu, gọi viewViewWillAppear:

- (void)passCustomer:(SCCustomer *)customer 
{ 
    self.customer = customer; 
    [self viewWillAppear:YES]; 
    [self dismissViewControllerAnimated:YES completion:^{}]; 
} 
+1

Cảm ơn danh sách, tôi đã thực hiện các thay đổi và đặt điểm ngắt trong chế độ xemWillAppear nhưng nó không tấn công. – guptron

+0

Tôi không biết về thay đổi hành vi trong iOS. Xem chỉnh sửa của tôi. – danh

+0

Yup, thêm mã vào phương thức ủy nhiệm đã hoạt động. Cảm ơn. – guptron

2

Bạn nên tải lại giao diện người dùng của bạn sau khi thiết lập các "khách hàng"

- (void)passCustomer:(SCCustomer *)customer 
{ 
    self.customer = customer; 

    //At this point, self.customer has the correct reference 

    [self dismissViewControllerAnimated:YES completion:^{ 
      // reload your UI here or call a method which will reload your ui 
      // e.g. for tableView it will be [tableView reload]; 
     }]; 
} 
+0

Cảm ơn bạn rất nhiều. Nó hoạt động như một sự quyến rũ đối với tôi. – Manthan

+0

Mã sẽ là gì để tải lại giao diện người dùng không sử dụng chế độ xem bảng tức là chỉ nhãn văn bản? – user1904273

+0

Ví dụ: label.text = customer.name –