【问题标题】:Get variable between Segues获取 Segue 之间的变量
【发布时间】:2013-03-02 14:40:15
【问题描述】:

我面临一个我不知道如何实施的情况。我的项目非常简单:

  1. 一个 ViewController 呈现一个带有特定 URL 的 webview;
  2. 在同一个视图控制器上,有一个按钮可以转到另一个由 segue 实现的视图控制器;
  3. 第二个 VC 已经加载了一个 tableview,所以当用户选择一个单元格时,我获取值并返回到主 viewController (step.1);

到目前为止它已经实现了,那么我需要实现的是当回到主 ViewController(step.1)时,我需要恢复在 step.3 中选择的值,将值附加到 URL 并重新加载 WebView零件。 我怎样才能做到这一点?我想知道这很简单,但我在研究中找不到。

提前感谢您的任何帮助,非常欢迎。 亚历克斯

【问题讨论】:

  • 你用的是什么类型的segue?推送还是模态?
  • 其实没关系。诀窍是能够访问第一个视图控制器中的变量(您要传递的值)。答案中解释了一种方法。另一种是使用应用程序委托或全局可访问的对象来存储变量。

标签: objective-c xcode webview tableview viewcontroller


【解决方案1】:

我也遇到了同样的问题……阅读了大量的教程和书籍。这对我有用。它混合了两种方法,可能只需要一种。但至少我理解它并且可以轻松使用它。这些方法是委托和使用单例来保存您的全局变量。

我将代码粘贴到 Word 中,删除了不适用的内容,并留下了您需要的所有内容。

首先,创建一个新的类文件。我的叫做 OptionsSingleton。这为您提供了 OptionsSingleton.h 和 OptionsSingleton.m。 我的有更多变量,但以下是适用于您的:

OptionsSingleton.h

@interface OptionsSingleton : NSObject{
int gblRowPicked;

}

@property(nonatomic) int gblRowPicked;

+(OptionsSingleton *) singleObj;

@end

OptionsSingleton.m

#import "OptionsSingleton.h"

@implementation OptionsSingleton
{
    OptionsSingleton * anotherSingle;
}
@synthesize gblRowPicked;

+(OptionsSingleton *)singleObj{

    static OptionsSingleton * single=nil;

    @synchronized(self)
    {
        if(!single)
        {
            single = [[OptionsSingleton alloc] init];
        }
    }
    return single;
}

@end

如果您告诉他们单例的存在,所有其他视图控制器都可以看到 gblRowPicked。

//  ViewControllerThatCallsTheTableViewController.h

#import <UIKit/UIKit.h>
#import "OptionsSingleton.h"

@interface ViewControllerThatCallsTheTableViewController.h  : UIViewController 
{

    OptionsSingleton *optionsSingle;
}

还有……

//  ViewControllerThatCallsTheTableViewController.m

#import "ViewControllerThatCallsTheTableViewController.h"
#import "OptionsSingleton.h"

@interface ViewControllerThatCallsTheTableViewController ()

@end

@implementation ViewControllerThatCallsTheTableViewController

- (void)viewDidLoad
{

    optionsSingle = [OptionsSingleton singleObj];

    [super viewDidLoad];

}

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if([segue.identifier isEqualToString:@"PickFromTable"]){

         TableViewController *viewController = segue.destinationViewController;
        viewController.delegate = self;
    }
}
-(void)done{
   [self dismissViewControllerAnimated:YES completion: nil];

    NSLog (@"Back in ViewControllerThatCallsTableViewController, Row picked is %i",optionsSingle.gblGamePicked);
   }

通过单击转场并在属性检查器中命名转场,在情节提要中输入转场标识符。关闭时 TableViewController 将调用 done 方法。这里所说的只是行值,以证明原始视图控制器知道该行。您的代码将从那里获取它。(如果需要,您可以传递行中的数据...您必须为此声明适当的全局变量。)

TableViewController 文件中有这些东西:

//  TableViewController.h


@protocol ViewControllerThatCallsTheTableViewControllerDelegate <NSObject>
-(void) done;
@end

@interface TableViewController

协议告诉 TableViewController “完成”方法在原始视图控制器中,而不是在 TableViewController 本身中。

//  TableViewController.m

#import "TableViewController.h"
#import "BeginningCell.h"  // used for the prototype cell
#import "OptionsSingleton.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad
{

      optionsSingle = [OptionsSingleton singleObj];

    //other stuff: arrays for filling the table, etc.
}

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

optionsSingle.gblRowPicked=indexPath.row+1;

     [self.delegate  done];

}

你去...我希望我在我的工作程序中找到了适用于你情况的所有代码。对不起,如果我遗漏了什么。

-罗伯

【讨论】:

    【解决方案2】:

    由于您在单击单元格时从 tableview 移回,因此您可以在 tableview 控制器的viewWillDisappeartableView:didSelectRowAtIndexPath:(委托方法)中实现它。

    你可以在第一个视图控制器中声明一个带有访问器的变量(比如 NSString 类型的返回值) 然后在 viewWillDisappear 或 tableView:didSelectRowAtIndexPath:` 中,您可以将变量传递给 presentingViewController 属性。

    FirstViewController.h

    @interface FirstViewController: UIViewController{
        NSString *returnedValue;
    }
    @property (strong) NSString *returnedValue;
    

    FirstViewController.m

    @implementation FirstViewController
    @synthesize returnedValue; 
    

    SecondViewController.m 中:

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
      FirstViewController *firstVC = (FirstViewController *)[self presentingViewController];
      firstVC.returnedValue = @"The value you want to pass";
    }
    

    然后在您的 FirstViewController 中,您可以在适当的方法中实现:

    NSURL *url = [NSURL urlWithString:@"%@",returnedValue"]; //according to your business logic
    [yourWebView loadURL:url];
    

    【讨论】:

    • 作为 Objective-c 的初学者,我明白你的意思,但是我仍然有一个问题,我应该在 FirstViewController 的何处或如何声明此变量,例如 @property NSString *selectedCategoryVC; ???尽管如此,我在 secondVC 上看不到这个属性。
    • 还有一个问题......我应该在 FirstViewController 上实现哪种方法来接收返回的值并重新加载 Web 视图?非常感谢您的帮助。
    • 我已经更新了答案。您可以使用任何方法,因为 self.returnedValue 已填充。例如,如果您想在返回视图时立即加载 Web 视图,则可以使用viewDidAppear:。或者您可以使用您用来加载 Web 视图的方法。由你决定。
    猜你喜欢
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多