![](/img/trans.png)
[英]iOS Pass data back from viewController2 to viewController 1 with presentModalSegue
[英]How do I update ViewController1 data with data passed back from ViewController2
我有一個NavigationVC可以加載表格視圖。 當您點擊一個單元格時,它會將一些數據復制到detailVC,其中有一個更新按鈕來更新值。 我如何刷新詳細信息VC中的數據,而又不返回到表格視圖並先進行更新?
這就是我想做的:
TableView> DetailsView> EditDetails>返回帶有更新信息的DetailsView
不是這個:
TableView> DetailsView> EditDetails> DetailsView(通過導航返回)> TableView>(拉動刷新)> DetailsView
編輯
- (void) receiveNotifications:(NSNotification *) notification {
if ([[notification name] isEqualToString:@"updateDetailsVC"]){
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSDictionary *userInfo = [notification userInfo];
NSLog (@"%@", userInfo);
PRIORITYstring = [userInfo objectForKey:@"PRIORITY"];
});
}
}
我能夠使代碼正常工作,並且通過NSLog,我的數據被作為字典推回。
當viewDidLoad獲取detailsVC時,它將運行將NSString放入其對應的UILabel的方法。
- (void)viewDidLoad {
[super viewDidLoad];
[self displayData];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotifications:)
name:@"updateDetailsVC"
object:nil];
}
-(void)displayData {
PRIORITYlabel.text = [NSString stringWithFormat:@"Short Description: \n%@", QDESCRIPTIONstring];
}
- (void) receiveNotifications:(NSNotification *) notification {
if ([[notification name] isEqualToString:@"updateDetailsVC"]){
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSDictionary *userInfo = [notification userInfo];
NSLog (@"%@", userInfo);
PRIORITYstring = [userInfo objectForKey:@"PRIORITY"];
//PRIORITYlabel.text = [userInfo objectForKey:@"PRIORITY"];
});
}
}
我將如何獲取字典來覆蓋初始數據?
您可以使用NSNotificationCenter
。 從編輯視圖發布通知,並從表視圖和詳細信息視圖訂閱相同的通知。
發布通知::
[[NSNotificationCenter defaultCenter]
postNotificationName:@"TestNotification"
object:nil userInfo: userInfo];
如果要從編輯視圖傳遞任何數據,則可以使用userInfo
詞典!!
從表視圖和詳細信息視圖訂閱通知::
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveTestNotification:)
name:@"TestNotification"
object:nil];
動作方法::
- (void) receiveTestNotification:(NSNotification *) notification
{
// [notification name] should always be @"TestNotification"
// unless you use this method for observation of other notifications
// as well.
if ([[notification name] isEqualToString:@"TestNotification"])
NSLog (@"Successfully received the test notification!");
//retrieve the userInfo dictionary received from edit view
NSDictionary *userInfo = [notification userInfo];
}
您可以在表視圖和詳細信息視圖中添加此操作方法。
在表視圖和詳細視圖中刪除觀察者::
- (void) dealloc
{
// If you don't remove yourself as an observer, the Notification Center
// will continue to try and send notification objects to the deallocated
// object.
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
編輯::
您可以更新displayData方法以將標簽文本作為參數傳遞。
-(void)displayData: (NSString*) labelText{
PRIORITYlabel.text = labelText;
}
您可以從viewDidLoad和receiveNotifications:方法調用相同的方法。
從viewDidLoad ::
[self displayData: [NSString stringWithFormat:@"Short Description: \n%@", QDESCRIPTIONstring]];
從receiveNotifications ::
[self displayData: [NSString stringWithFormat:@"Short Description: \n%@", [[notification userInfo] objectForKey:@"PRIORITY"]]];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.