XCode - 导航控制器推送 Segue - 在视图和跳过导航序列之间传递数据 [英] XCode - Navigation Controller Push Segue - Pass Data between Views and Skip Navigation Sequence

查看:44
本文介绍了XCode - 导航控制器推送 Segue - 在视图和跳过导航序列之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个关于这个故事板测试项目的问题.我是 Xcode 的新手,我找不到这个问题的解决方案.

I have 2 questions about this storyboard test project. I am new to Xcode, and I can't find solution to this problem.

这是故事板设置

视图控制器 A 是根视图控制器.视图 B 和视图 C 分别从它延伸出来.表视图从视图 C 扩展而来.视图 B 有 2 个按钮 - 到表视图和视图 B.(我忘了在视图 B 上添加 textField,然后从 B 转到 A)

View Controller A is Root View Controller. View B and View C extend from it separetely. Table View extends from View C. View B has 2 buttons - To Table View and to View B. (I forgot to add textField on View B, and segue from B to A)

1) 表视图按钮推动表视图.表格单元格包含一个字符串.按下单元格,视图返回到视图控制器 C,该字符串显示在文本字段中.

1) Button To Table View pushes Table View. Table Cell contains a string. Pressing the cell, view goes back to View Controller C and that string is displayed in text Field.

问题 1:如果我使用表视图中的模态 segue 来查看 C textField 将使用方法 prepareForSegue 进行更新,但我将丢失视图 C 上的导航栏.

problem 1: if I use modal segue from table view to view C textField will be updated with method prepareForSegue, but I will lose Navigation Bar on view C.

问题 2:如果我使用从 table View 到 View C 的 push segue,view C 上的后退按钮不会指向 view A,它会指向 table View.

problem 2: if I use push segue from table View to View C, back button on view C will not point to view A, it will point to table View.

解决方案?单击文本字段将其选中,然后按返回按钮更新视图 C.但是如何实现呢?

Solution? Click on the text field to select it, then press back button to update view C. But how to implement?

或者有更好的解决方案吗?

Or is there better solution?

2) 我想按视图 C 上的按钮转到视图 B.如果我使用 push segue,则视图 B 后退按钮将指向视图 C.如果我使用模态推送,视图 B 会丢失导航栏.

2) I want to press button on View C to go to view B. If I use push segue, View B Back button will point to view C. If I use modal push, view B loses Navigation bar.

我可以以某种方式将推segue推到视图B,它的后退按钮指向视图A吗?

Can I somehow make push segue to View B, that it's back button points to view A?

为了在视图控制器之间传递数据,我使用了 NSUserDefaults.事实证明,这是在视图之间传递简单数据(字符串、数字等)的非常有效的方式

to pass data between view Controllers, I used NSUserDefaults. Turns out that it is very efficient way of passing simple data (strings, numbers, etc) between Views

热用于 MODAL segue 吗?在所需的 tableViewController 中添加

hot to use it for MODAL segue? in desired tableViewController add in the

-(void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   //define user defaults
   NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
   NSString *testString = @"test";
   [defaults setObject:testString] forKey:@"test1"];
   [defaults synchronize];

   //dismiss ViewController
   [self dismissViewControllerAnimated:YES completion:nil]; }

如果你想使用 push segue 来关闭视图控制器并跳过导航序列(从序列中删除视图)替换

if you wish to use push segue to dismiss view controller and skip navigation sequence (drop the view from sequence) replace

   [self dismissViewControllerAnimated:YES completion:nil];

   [self.navigationController popViewControllerAnimated:YES];

推荐答案

当我们解决每个问题时,我会更新这个答案.

I will update this answer as we get through each problem.

如果您使用表格视图作为进行选择的一种方式,那么您应该关闭表格视图控制器,而不是以模态方式推送新视图.

If you are using the table view as a means to make a selection then you should instead Dismiss the table view controller instead of pushing a new view modally.

//In the table view controller's on cell selected method
[self dismissViewControllerAnimated:YES completion:^{
    //code
}];

如果您想从视图 C 返回视图 A,则推送视图将导致下一个视图位于顶部(如果您以模态方式呈现),您可以使用上面相同的代码从 C 返回到 A

Pushing views will result in the the next view being placed on top if you want to get back to view A from view C (Provided you presented it modally) you can use the same code above to get back from C to A

基本上在这种情况下:

View A --modal-> View C --modal-> TableViewController

然后你必须在 TableViewController 中调用一次上面的代码,在 ViewController C 中再次调用然后砰的一声回到A

Then you have to call the above code once in TableViewController and once again in ViewController C and bam back to A

就让 B 指向 A 而言,从 A 到 B 会更容易.但是,如果您真的不需要从 A 到 B,我建议更改结构.

As far as having B point back to A it's easier if you go from A to B However if you never really need to go from A to B I suggest changing up the structure.

但是为了满足您的要求,切换视图层次结构的一种方法是关闭视图 C,同时将 B 推到 A 之上.

But to fulfill your request one method of switching your view hierarchy is by dismissing view C while at the same time pushing B on top of A.

//Inside View C If you want it to happen only after the first animation completes just move the other line up into the block
[self dismissViewControllerAnimated:YES completion:^{
    //code
}];
[self.parentViewController.navigationController pushViewController:<#(UIViewController *)#> animated:YES];

我希望这会有所帮助.

这篇关于XCode - 导航控制器推送 Segue - 在视图和跳过导航序列之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆