2011-08-23 25 views
6

Trong đơn đăng ký của tôi, có hai viewControllers là FirstViewControllerDetailViewController. Khi nhấn vào ô bảng, nó điều hướng đến DetailViewController. Trong DetailViewController, tôi muốn chỉnh sửa và tải lại chế độ xem bảng của FirstViewControllerCách sử dụng NSNotification

Tôi làm cách nào để sử dụng NSNotification cho sự cố này?

Dưới đây là phương pháp tôi muốn thực hiện NSNotification thứ

-(IBAction) save{ 
strSelectedText=theTextField.text; 

[NSNotificationCenter defaultCenter]; 
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self]; 
[[NSNotificationCenter defaultCenter] postNotification:notification]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (objFirstViewController) name:@"MyNotification" object:nil]; 



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

Có thể hữu ích cho bạn http://mac-objective-c.blogspot.com/2009/02/nsnotifications-broadcasting-mechanism.html – Tendulkar

+0

Đây không phải là nhiệm vụ để thông báo. Chỉ cần nhìn vào TableViewProgrammingGuide. Bạn có thể bắt đầu tại đây: http://developer.apple.com/library/mac/#documentation/cocoa/Conceptual/TableView/Introduction/Introduction.html HTH –

+0

Tôi khuyên bạn nên sử dụng mẫu đại biểu hơn là thông báo. –

Trả lời

9
-(void)viewDidLoad { 

[NSNotificationCenter defaultCenter]; 
NSNotification* notification = [NSNotification notificationWithName:@"MyNotification" object:self]; 
[[NSNotificationCenter defaultCenter] postNotification:notification]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (objFirstViewController) name:@"MyNotification" object:nil]; 

} 


-(IBAction) save{ 

[[NSNotificationCenter defaultCenter] postNotificationName:MyNotification object:sender]; 

//this will go to where you implement your selector objFirstViewController. 

} 

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

} 
0

bài thông báo từ detailViewController và thêm firstViewController như người quan sát.

Đảm bảo bạn xóa fireViewController khỏi danh sách người quan sát khỏi viewDidUnload.

Ngay bây giờ, bạn đang thêm detailViewController làm người quan sát.