Swift中是否有类似"If the storyboard ID is XXX, then..."的代码?

Is there any code like "If the storyboard ID is XXX, then..." in Swift?

我现在正在构建一个应用程序,它需要在两个 UIView Controllers 一个 UIWebView 中加载两个不同的 URL。但是,它们需要在相同的 类 中。那么有什么方法可以简单地检查 "If the storyboard ID is XXX, then load URL_1, else URL_2"?

这是我的代码:

let day_url = NSURL(string: "http://www.url1.com/")
let day_url_request = NSURLRequest(URL: day_url!)
day_webView.loadRequest(day_url_request)


let week_url = NSURL(string: "http://www.url2.com/")
let week_url_request = NSURLRequest(URL: week_url!)
week_webView.loadRequest(week_url_request)

谢谢!

PS:抱歉我的英语不好。 :(

您的每个 UIViewController 都有一个唯一 ID 和一个自定义 class。第一个 UIViewController 应该看起来像这样:

class StoryboardID1: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let day_url = NSURL(string: "http://www.url1.com/")
        let day_url_request = NSURLRequest(URL: day_url!)
        day_webView.loadRequest(day_url_request)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

对于第二个 UIViewController 我们有:

class StoryboardID2: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let week_url = NSURL(string: "http://www.url2.com/")
        let week_url_request = NSURLRequest(URL: week_url!)
        week_webView.loadRequest(week_url_request)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

反之

首先,声明您的 UIViewController:

let myViewControllerID = self.storyboard?.instantiateViewControllerWithIdentifier("PutIDHere") as UIViewController

然后检查 myViewControllerID 是否真的是用户导航的控制器:

if segue.identifier == "myViewControllerID" {
    ...
}

您可以使用 restorationIdentifier 而不是 Storyboard 标识符并从

中检查它
if self.restorationIdentifier == "View1" {
     // load URL1
} else {
     // Load URL2
}

祝你好运