ios - 如何以编程方式将 UIBarButtonItem 添加到拖到 View 上的 UINavigationBar

标签 ios xcode swift uinavigationbar uinavigationitem

我已经搜索并查看了针对此问题的多种解决方案,但似乎没有任何适合我的方法。这是我的代码和屏幕截图。如果有什么不同,我手动将导航栏拖到 View 上并连接了一个 socket 。谢谢。我很感激这方面的任何帮助。 enter image description here

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
    }




}

最佳答案

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

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

self.navigationBarTitle.topItem?.leftBarButtonItem = backButton

关于ios - 如何以编程方式将 UIBarButtonItem 添加到拖到 View 上的 UINavigationBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29271577/

相关文章:

ios - Swift:多点连接 + UI 更新

ios - 如何在 iOS 8/Swift 中处理带有屏幕尺寸/方向变化的 NSLayoutConstraints?

iphone - 用于 arm7/iOS 4.3 的 pjsip 构建 - 在 mips_test.o 中找不到符号

swift - UserDefaults 的多个设置导致显示速度变慢

swift - 协议(protocol)扩展中的嵌套结构 : Type '...' cannot be nested in generic function '...()'

ios - 使用 NSURLSession 自定义委托(delegate)时不调用身份验证质询方法

ios - 删除后退按钮文本和居中文本

iphone - 仪器在 xcode 4.2 模拟器中不起作用

iphone - 有没有办法在 iOS 应用程序中嵌入 Excel 图表?

ios - 替换 iOS .ipa 文件的数据容器