Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios setRightBarButtonItems不';行不通_Ios_Swift_Uinavigationitem - Fatal编程技术网

Ios setRightBarButtonItems不';行不通

Ios setRightBarButtonItems不';行不通,ios,swift,uinavigationitem,Ios,Swift,Uinavigationitem,我正在尝试在UINavigationBar右侧设置两个按钮。下面是源代码。没有错误,但也没有按钮。它是一个UIViewController,而不是UINavigationViewController let buttonEdit: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton buttonEdit.frame = CGRectMake(0, 0, 40, 40) buttonEdit.setImage(U

我正在尝试在
UINavigationBar
右侧设置两个按钮。下面是源代码。没有错误,但也没有按钮。它是一个
UIViewController
,而不是
UINavigationViewController

let buttonEdit: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
buttonEdit.frame = CGRectMake(0, 0, 40, 40)
buttonEdit.setImage(UIImage(named:"me44.png"), forState: UIControlState.Normal)
buttonEdit.addTarget(self, action: "rightNavItemEditClick:", forControlEvents: UIControlEvents.TouchUpInside)
var rightBarButtonItemEdit: UIBarButtonItem = UIBarButtonItem(customView: buttonEdit)

let buttonDelete: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
buttonDelete.frame = CGRectMake(0, 0, 40, 40)
buttonDelete.setImage(UIImage(named:"me44.png"), forState: UIControlState.Normal)
buttonDelete.addTarget(self, action: "rightNavItemDeleteClick:", forControlEvents: UIControlEvents.TouchUpInside)

var rightBarButtonItemDelete: UIBarButtonItem = UIBarButtonItem(customView: buttonDelete)

// add multiple right bar button items       
self.navigationController?.navigationItem.setRightBarButtonItems([rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)

//I also tried code below no luck either
self.navigationItem.setRightBarButtonItems([rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)

这个代码是错误的:

self.navigationController?.navigationItem.setRightBarButtonItems(
    [rightBarButtonItemDelete, rightBarButtonItemEdit] as [AnyObject], animated: true)
您没有设置导航控制器的
导航项
;您可以设置
navigationItem
。而且,
[AnyObject]
这个东西是不必要的。因此:

self.navigationItem.setRightBarButtonItems(
    [rightBarButtonItemDelete, rightBarButtonItemEdit], animated: true)
但是,请注意,仅当视图控制器是UINavigationController的子级时,这种方法才有效。设置视图控制器的
navigationItem
仅在这种情况下自动填充导航栏。如果您不在这种情况下-即,如果您的界面中只有一个“松散”的导航栏-您需要手动填充导航栏(通过设置其
导航项


(另外,请注意,如果您没有
“me44.png”
图像,可能是您的代码正在工作,但您什么也看不到。)

尽管前面已经指出了所有这些,但您可能看不到导航栏上添加的按钮。对于Swift 3,请在AppDelegate类中尝试此操作(请注意topItem):


它是UINavigationController中的UIViewController吗?多亏了你的信息,我使用了self.navBar.topItem?.RightBarButtonims=[RightBarButtonimDelete,RightBarButtonimedIt],效果非常好。然而,大多数人所做的是使用UINavigationController,即使没有任何导航。这样,您就可以使用第一种方法。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    window = UIWindow()
    window?.backgroundColor = UIColor.white

    let navigationVC = UINavigationController(rootViewController: UIViewController())

    let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTodo))
    navigationVC.navigationBar.topItem?.rightBarButtonItem = addButton

    window?.rootViewController = navigationVC
    window?.makeKeyAndVisible()

    return true
}