我有一个通用的UIViewController所有的uiviewscontroller都扩展了它来重用一些通用的操作。

我想在这个Common UIViewController上建立一个segue这样所有其他的UIViewController都会继承。

我想知道如何通过编程来实现。

我想这个问题也可能是我如何为我所有的uiviewcontroller设置segue而不需要进入故事板手工操作。


当前回答

我对UIStoryboard的segue进行了逆向工程,并做了一个开源(重新)实现:https://github.com/acoomans/Segway

有了这个库,就可以编程地定义segue(不需要任何故事板)。

希望能有所帮助。

其他回答

你必须将你的代码链接到你正在使用的UIStoryboard。确保你进入UIStoryboard中的YourViewController,点击它周围的边框,然后设置它的标识符字段为你在代码中调用的NSString。

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" 
                                                     bundle:nil];
YourViewController *yourViewController = 
 (YourViewController *)
  [storyboard instantiateViewControllerWithIdentifier:@"yourViewControllerID"];
[self.navigationController pushViewController:yourViewController animated:YES];

我想我可以加上另一种可能性。你能做的一件事是你能用一个没有附加到动作的segue连接故事板中的两个场景,然后在你的视图控制器中以编程方式触发这个segue。你这样做的方法是,你必须从故事板场景底部的文件所有者图标拖拽到segue场景,然后右拖拽到目标场景。我将附上一张图片来帮助解释。

弹出窗口将显示“手动Segue”。我选择Push作为类型。点击小方块,确保你在属性检查器中。给它一个标识符,用于在代码中引用它。

接下来我要用一个程序化的栏按钮项进行segue。在viewDidLoad或其他地方,我将用下面的代码在导航栏上创建一个按钮项:

UIBarButtonItem *buttonizeButton = [[UIBarButtonItem alloc] initWithTitle:@"Buttonize"
                                                                    style:UIBarButtonItemStyleDone
                                                                   target:self
                                                                   action:@selector(buttonizeButtonTap:)];
self.navigationItem.rightBarButtonItems = @[buttonizeButton];

注意到选择器是buttonizeButtonTap:。所以为那个按钮写一个void方法在这个方法中你会像这样调用segue:

-(void)buttonizeButtonTap:(id)sender{
    [self performSegueWithIdentifier:@"Associate" sender:sender];
    }

当调用prepareForSegue时,需要sender参数来标识按钮。prepareForSegue是框架方法,你将实例化你的场景,并传递给它任何值,它将需要做它的工作。下面是我的方法:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"Associate"])
    {
        TranslationQuizAssociateVC *translationQuizAssociateVC = [segue destinationViewController];
        translationQuizAssociateVC.nodeID = self.nodeID; //--pass nodeID from ViewNodeViewController
        translationQuizAssociateVC.contentID = self.contentID;
        translationQuizAssociateVC.index = self.index;
        translationQuizAssociateVC.content = self.content;
    }
}

我测试过了,效果很好。

你可以创建UIStoryBoardSegue的子类。子类化主要用于提供自定义过渡动画。

你可以看到wwdc 2011介绍StoryBoard的视频。YouTube上也有。

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIStoryboardSegue_Class/Reference/Reference.html#//apple_ref/occ/cl/UIStoryboardSegue

下面是编程方式创建segue的代码示例:

class ViewController: UIViewController {
    ...
    // 1. Define the Segue
    private var commonSegue: UIStoryboardSegue!
    ...
    override func viewDidLoad() {
        ...
        // 2. Initialize the Segue
        self.commonSegue = UIStoryboardSegue(identifier: "CommonSegue", source: ..., destination: ...) {
            self.commonSegue.source.showDetailViewController(self.commonSegue.destination, sender: self)
        }
        ...
    }
    ...
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // 4. Prepare to perform the Segue
        if self.commonSegue == segue {
            ...
        }
        ...
    }
    ...
    func actionFunction() {
        // 3. Perform the Segue
        self.prepare(for: self.commonSegue, sender: self)
        self.commonSegue.perform()
    }
    ...
}

我对UIStoryboard的segue进行了逆向工程,并做了一个开源(重新)实现:https://github.com/acoomans/Segway

有了这个库,就可以编程地定义segue(不需要任何故事板)。

希望能有所帮助。