3

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

图片在这里: 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";
}
4

7 回答 7

4

对于 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)
    
于 2017-05-31T09:22:59.540 回答
1

我有第一个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";
                } }

希望对你有帮助!

于 2016-11-14T18:42:26.620 回答
0

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

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

于 2015-03-07T15:15:23.147 回答
0

如果您在情节提要中设置前视图控制器,则在调用 loadView 之前不会在 SWRevealViewController 中分配它。只需添加

[destinationViewController loadView];

访问前destinationViewController.frontViewController

于 2015-08-03T18:03:45.263 回答
0

使用 Swift 你可以这样做:

  • UIButton首先,您可以在 a和 a之间创建一个 segue ViewController,要做到这一点,您应该使用您的故事板,选择您的元素并Ctrl拖动到您的视图。

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

弹出segue swift

并在 Main.storyboard 上选择新的 segue

在此处输入图像描述

  • 然后在你的右边板上你必须初始化一个IdentifierClass一个SWRevealViewControllerSeguePushController

在此处输入图像描述

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

    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"
        }
    }

希望有帮助,

于 2016-06-08T08:39:43.550 回答
0

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

    let revealViewCOntroller = segue.destinationViewController as! SWRevealViewController
    revealViewCOntroller.loadView()
    let secondVc = revealViewCOntroller.frontViewController as! SecondVC
    secondVc.loadView()
    secondVc.strToken = "messageToPass"
于 2016-03-21T04:58:36.873 回答
-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";

}
}
于 2014-09-01T12:55:26.023 回答