Swift 2如何更改public var string

时间:2015-10-06 14:29:19

标签: ios string swift2

我有Menu类,我有另一个视图,我想在另一个项目中更改菜单类标题项目。

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)

我添加了所有需要你帮助的代码。非常感谢。

1 个答案:

答案 0 :(得分:1)

在设置标题后,

在会话的响应块中为菜单设置let声明。当前声明不会获得新标题,因为请求是以异步方式启动的。

这意味着请求正在运行,代码仍在前进。

无论如何,你尝试做什么看起来很奇怪。想想在调用项目的管理器上工作,并使用Controller来调用它。通过响应,您应该创建导航控制器并将其填入您的项目。仍然很奇怪,但导航控制器是为导航而设计的;)