我有菜單類,我有另一個視圖,我想在另一個項目中更改菜單類標題項我的代碼在這裏。Swift 2如何更改公共變量字符串
Menuitemfile。
public class MediumMenuItem: NSObject {
public var title:String?
public var completion: completionHandler?
override private init() {
super.init()
}
public convenience init(title: String, completion: completionHandler) {
self.init()
self.title = title
self.completion = completion
}
}
配置文件
var title1 = ""
var title2 = ""
var title3 = ""
視圖控制器
class NavigationController: UINavigationController {
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var menu: MediumMenu?
override func viewDidLoad() {
super.viewDidLoad()
let myURL: NSURL! = NSURL(string: "http://bla.com/title_api.php")
let request: NSMutableURLRequest = NSMutableURLRequest(URL: myURL!)
request.HTTPMethod = "POST"
let postString = "title=1"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("error=\(error)")
return
}else{
if let unwrappedData = data {
let string1 = NSString(data: unwrappedData, encoding: NSUTF8StringEncoding)
let fullNameArr = string1!.componentsSeparatedByString(",")
title1 = fullNameArr[0]
title2 = fullNameArr[1]
title3 = fullNameArr[2]
topURL1 = fullNameArr[3]
topURL2 = fullNameArr[4]
topURL3 = fullNameArr[5]
}
}
}
task.resume()
let item2 = MediumMenuItem(title: title1) {
let topStoriesViewController = storyboard.instantiateViewControllerWithIdentifier("Web1") as! WebViewController1
self.setViewControllers([topStoriesViewController], animated: false)
}
let item3 = MediumMenuItem(title: title2) {
let bookMarksViewController = storyboard.instantiateViewControllerWithIdentifier("Web2") as! WebViewController2
self.setViewControllers([bookMarksViewController], animated: false)
}
let item4 = MediumMenuItem(title: title3) {
let bookMarksViewController = storyboard.instantiateViewControllerWithIdentifier("Web3") as! WebViewController3
self.setViewControllers([bookMarksViewController], animated: false)
}
}
遠程JSON文件輸出
New1,New2,New3,http://www.bla.com,http://www.bla.com,http://www.bla.com
Xcode的輸出
No error and title1,title2,title3 showing blank in my app (not showing)
我說我需要你的幫助非常感謝所有代碼。
@有任何想法嗎? – SwiftDeveloper