是否有像#34的代码;如果故事板ID是XXX,那么......"在斯威夫特?

时间:2015-01-22 14:51:15

标签: ios swift uiwebview

我现在正在构建一个应用,需要在两个UIView ControllersUIWebView内加载两个不同的网址。虽然,他们需要在同一个班级。那么有什么方法可以简单地检查"如果故事板ID是XXX,那么加载URL_1,否则加载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:抱歉我的英语不好。 :(

Two View Controller in 1 class but 2 IDs

2 个答案:

答案 0 :(得分:0)

您的每个UIViewController都有唯一的ID和自定义类。第一个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" {
    ...
}

答案 1 :(得分:0)

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

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

祝你好运