如何以编程方式将 UIBarButtonItem 添加到拖到视图上的 UINavigationBar

Posted

技术标签:

【中文标题】如何以编程方式将 UIBarButtonItem 添加到拖到视图上的 UINavigationBar【英文标题】:How to add a UIBarButtonItem programmatically to UINavigationBar dragged onto view 【发布时间】:2015-03-26 05:19:10 【问题描述】:

我已经搜索并查看了此问题的多种解决方案,但似乎没有任何适合我的方法。这是我的代码和屏幕截图。如果有什么不同,我手动将导航栏拖到视图上并连接了插座。谢谢。感谢您对此的任何帮助。

class SubjectsViewController: UIViewController 

    @IBOutlet var myTableView: UITableView!

    @IBOutlet var noBooksLabel: UILabel!

    @IBOutlet var navigationBarTitle: UINavigationBar!

    func backAction() -> Void 

        self.navigationController?.popViewControllerAnimated(true)

    

    override func viewWillAppear(animated: Bool) 

        self.noBooksLabel.hidden = true

    

    override func viewDidLoad() 
        super.viewDidLoad()

        descriptions.removeAll(keepCapacity: true)
        usernames.removeAll(keepCapacity: true)
        imageFiles.removeAll(keepCapacity: true)

        println("Subject view controller")

        self.navigationBarTitle.topItem?.title = "\(selectedSubject)"

        let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")

        self.navigationItem.leftBarButtonItem = backButton


        pickedSubject = selectedSubject

        println("Picked Subject = \(pickedSubject)")

        println("Subject view controller")

        self.navigationBarTitle.topItem?.title = "\(selectedSubject)"

        //let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "backAction")
        //self.navigationItem.leftBarButtonItem=barBtn;

        var subjectQuery = PFQuery(className: "BookPosting")

        subjectQuery.whereKey("CourseSubject", equalTo: pickedSubject)
        subjectQuery.findObjectsInBackgroundWithBlock 

            (objects: [AnyObject]!, error: NSError!) -> Void in

            if error == nil 

                // The find succeeded.
                println("Successfully retrieved \(objects.count) scores.")

                // Do something with the found objects
                if let objects = objects as? [PFObject] 

                    for object in objects 

                        descriptions.append(object["Description"] as String)

                        usernames.append(object["username"] as String)

                        imageFiles.append(object["imageFile"] as PFFile)

                        self.myTableView.reloadData()

                    

                

                println("descriptions.count = \(descriptions.count)")



                if (descriptions.count == 0) 

                    self.noBooksLabel.hidden = false

                 else 

                    self.noBooksLabel.hidden = true

                

             else 

                // Log details of the failure
                println("Error: \(error) \(error.userInfo!)")

            

        

    

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 

        return descriptions.count

    

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat 

        return 100

    

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell 

        var cell: bookCell = tableView.dequeueReusableCellWithIdentifier("booksCells") as bookCell


        cell.subjectBookDescription.text = descriptions[indexPath.row]
        cell.subjectBookPosterUsername.text = usernames[indexPath.row]

        imageFiles[indexPath.row].getDataInBackgroundWithBlock  (imageData: NSData!, error: NSError!) -> Void in

            if error == nil 

                let image = UIImage(data: imageData)

                cell.subjectBookImage.image = image

            

        

        return cell
    





【问题讨论】:

【参考方案1】:

我假设您正在尝试将 UIBarButtonItem 添加到连接到插座 navigationBarTitle 的导航栏。

self.navigationItem.leftBarButtonItem = backButton 替换为以下内容:

self.navigationBarTitle.topItem?.leftBarButtonItem = backButton

【讨论】:

以上是关于如何以编程方式将 UIBarButtonItem 添加到拖到视图上的 UINavigationBar的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式将 UIBarButtonItem 添加到拖到视图上的 UINavigationBar

如何以编程方式禁用/启用UIBarButtonItem

以编程方式在Swift中添加UIBarButtonItem操作

以编程方式添加UIBarButtonItem

以编程方式创建和显示 UIBarButtonItem 的正确方法是啥?

以编程方式从 UIBarButtonItem