2015-10-06 54 views
1

我有菜單類,我有另一個視圖,我想在另一個項目中更改菜單類標題項我的代碼在這裏。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) 

我說我需要你的幫助非常感謝所有代碼。

+0

@有任何想法嗎? – SwiftDeveloper

回答

1

在設置標題後,請在會話的響應塊中重新設置菜單的聲明。當前的聲明不會得到新的標題,因爲請求是異步啓動的。

這意味着請求正在運行,並且代碼仍在繼續前進。

無論如何,它看起來很奇怪你試圖做什麼。想象一下管理員可以完成調用項目的工作,並使用Controller來調用它。有了這個迴應,你應該創建你的導航控制器並填寫你的項目。仍然奇怪,但導航控制器是爲導航;)

+0

:)是的,我認爲所以導航控制器類也有我從那裏添加控制器代碼:)我認爲需要專家代碼。仍在等待:-II – SwiftDeveloper

+0

好吧,在響應塊中創建你的項目,你應該繼續。但是你真正想要的是有人爲你或者什麼寫菜單? –