SWRevealViewController传递参数

时间:2014-07-11 15:45:17

标签: ios swrevealviewcontroller

通过SWRevealViewController将参数从一个ViewController传递到其他ViewController。这不是关于在Front Controller和Rear Controller之间传递参数,而是从另一个ViewController(firstViewController)到前端(frontViewController)。 见图片以供参考。 segue(按钮触摸时)尝试将字符串值传递给前端控制器

图片在这里:http://portwire.com/stackoverflow/storyboard.png

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SWRevealViewController *destinationViewController = (SWRevealViewController *)segue.destinationViewController;
    UINavigationController *navegationController = (UINavigationController *)destinationViewController.frontViewController;

    //UUPS!! SWRevealViewController.frontViewController is nil

    //How to access destination UIViewController????????

    FrontViewController * frontViewController = (FrontViewController *)navegationController.topViewController;
    frontViewController.fromString = @"String from previous view";
}

7 个答案:

答案 0 :(得分:2)

对于Swift 3.0:

  1. 您必须将SWRevealViewController的storyBoardId命名为您想要的任何内容,在此示例中:“SWRevealViewController”

  2. 请遵循以下代码:

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let revealVc = storyboard.instantiateViewController(withIdentifier :"SWRevealViewController") as! SWRevealViewController
    revealVc.loadView()
    let navViewController = revealVc.frontViewController as! UINavigationController
    if (navViewController.topViewController?.isKind(of: MainVc.superclass()!)) {
    let mainVc = navViewController.topViewController as! MainVc
    mainVc.email = email
    }
    self.present(revealVc, animated: true, completion: nil)
    

答案 1 :(得分:0)

我认为您可以使用委托来做到这一点。

在前视图控制器中定义协议,让第一个视图控制器符合它。

答案 2 :(得分:0)

如果在故事板中设置前视图控制器,则在调用loadView之前,不会在SWRevealViewController中分配它。只需添加

[destinationViewController loadView];

访问destinationViewController.frontViewController

之前

答案 3 :(得分:0)

我希望这可以帮助你......

    let revealViewCOntroller = segue.destinationViewController as! SWRevealViewController
    revealViewCOntroller.loadView()
    let secondVc = revealViewCOntroller.frontViewController as! SecondVC
    secondVc.loadView()
    secondVc.strToken = "messageToPass"

答案 4 :(得分:0)

使用Swift你可以这样做:

  • 首先,你可以在UIButtonViewController之间创建一个segue,为此你应该使用你的故事板,选择你的元素并用Ctrl拖到你的观点。

  • 会出现一个弹出窗口,因此您必须选择Custom

Pop-up segue swift

然后在Main.storyboard上选择新的segue

enter image description here

  • 然后在您的右侧董事会上,您必须使用Identifier
  • 初始化ClassSWRevealViewControllerSeguePushController

enter image description here

  • 最后,在视图控制器中添加这样的参数。

    class NewViewController: UIViewController {
        var myParam: String!
    }
    

以这种方式发送参数:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "showMyNewView") {
            let detailScene = segue.destinationViewController as! NewViewController
            detailScene.myParam = "ParamToSend"
        }
    }

希望有所帮助,

答案 5 :(得分:0)

我有LoginViewController -> SWRevealViewController -> NavigationController -> HomeViewController(HomeViewController是frontViewController)

从LoginViewController到SWRevealViewController我有一个segue。为了传递数据,我在prepareForSegue中使用了适用于我的代码。

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

   SWRevealViewController *destination = [segue destinationViewController];
   [destination loadView];
   UINavigationController *navViewController = (UINavigationController *) [destination frontViewController];

   if ([navViewController.topViewController isKindOfClass:[HomeViewController class]]) {

        HomeViewController* homeVC = (HomeViewController*)navViewController.topViewController;
        homeVC.title = @"Test1";
                } }

希望能帮到你!

答案 6 :(得分:-1)

首先,您需要在前视图中为NSString声明@property。

比用segue方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"showDetails"]) {

    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    DetailViewController *dvc = segue.destinationViewController;
    dvc.str = @"Your current view's string";

}
}