在“配方”中添加的Xamarin.IOS UIBarbuttonItem不起作用。没有错误没有按钮

时间:2018-11-06 19:20:28

标签: xamarin.ios uinavigationbar uibarbuttonitem

使用从“选项卡式应用程序”模板派生的Xamarin IOS应用程序。针对IOS 11+;在模拟器中测试12.1。

我向UIView选项卡之一添加了一个UINavigationBar,并试图在该栏中添加一个保存和取消按钮。

没有错误,没有乐趣,NavigationItem不为null,并显示正确的导航项目。这是大纲:

--UIViewController MyInfoController
----UIView InfoView
------UINavigationBar NavBar
--------UINavigationItem [no name]
------UIView ContentView
------A bunch of constraints
----UITabBar 

这是相关代码段:

    public partial class MyInfoController : UIViewController
{
    protected MyInfoController(IntPtr handle) : base(handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.

        this.NavigationItem.SetRightBarButtonItem(
            new UIBarButtonItem(UIBarButtonSystemItem.Save, (sender, args) => 
            {
                // Handle Save Button Click here.
                //Create Alert
                var okAlertController = UIAlertController.Create("Click", "Right Button Clicked.", UIAlertControllerStyle.Alert);
                //Add Action
                okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
                // Present Alert
                PresentViewController(okAlertController, true, null);
            }), true);


        // Try another way...

        var button = new UIBarButtonItem(UIBarButtonSystemItem.Cancel,  (sender, e) =>
        { 
            //Put Something here 
        });

        button.SetTitleTextAttributes(new UITextAttributes()
        {
            TextColor = UIColor.White,
            TextShadowColor = UIColor.Clear
        }, UIControlState.Normal);

        this.NavigationItem.SetLeftBarButtonItem(button, true);
        // Create and add Views

没有错误消息问题。我没有看到任何错误,但也没有看到Bar按钮项。

我想念什么? (:-S)

1 个答案:

答案 0 :(得分:1)

如果您使用ios Designer添加UINavigationBarUINavigationItem,则最好为它们设置名称,例如

------UINavigationBar NavBar
--------UINavigationItem BarItem

因此,在控制器中,栏将正常显示。

 public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();
        // Perform any additional setup after loading the view, typically from a nib.
        this.BarItem.SetRightBarButtonItem(
        new UIBarButtonItem(UIBarButtonSystemItem.Save, (sender, args) =>
        {
            // Handle Save Button Click here.
            //Create Alert
            var okAlertController = UIAlertController.Create("Click", "Right Button Clicked.", UIAlertControllerStyle.Alert);
            //Add Action
            okAlertController.AddAction(UIAlertAction.Create("OK", UIAlertActionStyle.Default, null));
            // Present Alert
            PresentViewController(okAlertController, true, null);
        }), true);


        // Try another way...

        var button = new UIBarButtonItem(UIBarButtonSystemItem.Cancel, (sender, e) =>
        {
            //Put Something here 
        });

        button.SetTitleTextAttributes(new UITextAttributes()
        {
            TextColor = UIColor.Black,
            TextShadowColor = UIColor.Clear
        }, UIControlState.Normal);

        this.BarItem.SetLeftBarButtonItem(button, true);
    }

更多信息:

如果使用this.NavigationItem,则根控制器中常用的是UINavigationController