gpt4 book ai didi

ios - 重新加载 UINavigationItem

转载 作者:行者123 更新时间:2023-11-30 14:13:04 27 4
gpt4 key购买 nike

我想用一个 navItem.rightBarButtonItem 加载 View Controller ,并根据一组情况将其更改为另一个。似乎我只能在 viewDidLoad 上加载它一次。更改图标后是否有更新栏按钮项目的方法?

func viewDidLoad() {
var iconOne = UIImage(named: "button")
let buttonOne = UIBarButtonItem(image: iconOne, style: UIBarButtonItemStyle.Plain, target: self, action: "funcOne")
self.navItem.rightBarButtonItem = buttonOne

//perform async query, if a condition holds true then:

var iconTwo = UIImage(named: "buttonTwo")
let buttonTwo = UIBarButtonItem(image: iconTwo, style: UIBarButtonItemStyle.Plain, target: self, action: "funcTwo")
self.navItem.rightBarButtonItem = buttonTwo
}

完整代码:

override func viewDidLoad() {
super.viewDidLoad()


// Sets custom nav bar view so we can add multiple bar buttons
navBar = UINavigationBar(frame: CGRectMake(0, 20, UIScreen.mainScreen().bounds.size.width, 44))
navBar.barTintColor = UIColor.blackColor() // Sets bar to black
navBar.translucent = false
self.view.addSubview(navBar)
navItem = UINavigationItem(title: "\(name)") // Sets title
navBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 25)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
// Set icons
var backIcon = UIImage(named: "backButton")
let backButton = UIBarButtonItem(image: backIcon, style: UIBarButtonItemStyle.Plain, target: self, action: "backButton")
navItem.leftBarButtonItem = backButton

var userQuery = PFQuery(className: "Followers")
userQuery.whereKey("following", equalTo: username)
userQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {

if let objects = objects {

for object in objects {

if let follower = object["follower"] as? String {
if follower == PFUser.currentUser()!.username! {
var followingIcon = UIImage(named: "followingButton")
let followingButton = UIBarButtonItem(image: followingIcon, style: UIBarButtonItemStyle.Plain, target: self, action: "unfollowUser")
self.navItem.rightBarButtonItem = followingButton

self.navItem.rightBarButtonItem?.tintColor = UIColor.orangeColor()
} else {

var followIcon = UIImage(named: "followButton")
let followButton = UIBarButtonItem(image: followIcon, style: UIBarButtonItemStyle.Plain, target: self, action: "followUser")
self.navItem.rightBarButtonItem = followButton


self.navItem.rightBarButtonItem?.tintColor = UIColor.darkGrayColor()
}
}
}

}
} else {
println(error)
}
})

最佳答案

我不确定这是否可行。因此,我制作了自己的自定义 View ,其中每个栏按钮项目都在其中。我将右栏按钮项目设置为 var rightButton = UIButton(frame: CGRectMake(UIScreen.mainScreen().bounds.width - 50, 25, 30, 30)) ,添加一个 Action ,并将 subview 添加到 View 中。

关于ios - 重新加载 UINavigationItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31530498/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com