objective-c - 在导航栏上添加自定义 View

标签 objective-c ios ipad uinavigationbar

这是我的代码。

- (void)viewWillAppear:(BOOL)animated
{

    customView = [[UIView alloc] init];

    UIButton *btnAdd = [[UIButton alloc] initWithFrame:CGRectMake(410, -20, 30, 40)];
    [btnAdd setBackgroundColor:[UIColor blackColor]];
    [btnAdd setBackgroundImage:[UIImage imageNamed:@"oie_815362uTrAOBMX.png"] forState:UIControlStateNormal];
    [btnAdd addTarget:nil action:@selector(addCustomer:) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:btnAdd];

    UIButton *btnJump = [[UIButton alloc] initWithFrame:CGRectMake(450, -20, 30, 40)];
    [btnJump setBackgroundColor:[UIColor blueColor]];
    [btnJump setBackgroundImage:[UIImage imageNamed:@"oie_8153842yVgkR6XD.jpg"] forState:UIControlStateNormal];
    [btnJump addTarget:nil action:@selector(showMenue:) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:btnJump];

    UIButton *btnBarItem = [[UIButton alloc] initWithFrame:CGRectMake(300, -20, 100, 40)];
    [btnBarItem setBackgroundColor:[UIColor greenColor]];
    [btnBarItem setBackgroundImage:[UIImage imageNamed:@"oie_99342oliBc5Sy.jpg"] forState:UIControlStateNormal];
    [btnBarItem addTarget:nil action:@selector(showScanner:) forControlEvents:UIControlEventTouchUpInside];
    [customView addSubview:btnBarItem];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, -10, 30, 30)];
    [imageView setImage:[UIImage imageNamed:@"oie_972931iW4SHiZU.jpg"]];
    [customView addSubview:imageView];

    UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, -20, 80, 40)];
    [mainLabel setText:@"Kunden"];
    [mainLabel setBackgroundColor:[UIColor lightGrayColor]];
    [mainLabel setTextColor:[UIColor blackColor]];
    [mainLabel setFont:[UIFont fontWithName:@"Arial" size:18]];
    [customView addSubview:mainLabel];

    [self.navigationItem setTitleView:customView];
}

- (IBAction)showMenue:(id)sender
{
    NewPopUpmenu *options = [[NewPopUpmenu alloc] initWithNibName:@"PopupMenu" bundle:nil];
    [options.view setFrame:CGRectMake(720, 0, 300, 200)];
    [self.view addSubview:options.view];
}

注意:问题是当我按下“跳转”按钮时,它不会访问它的选择器方法 (showMenue:)。

最佳答案

您正在为每个按钮的目标操作对指定 nil 的目标。如果你的 Action 方法是在同一个类中实现的,你的target应该是self:

[btnAdd addTarget:self
           action:@selector(addCustomer:)
 forControlEvents:UIControlEventTouchUpInside];
// ...
// Same for other buttons...
// ...

从技术上讲,如果您为 target 参数指定 nil,则会在响应链中搜索响应该操作的对象。您遇到的问题可能是因为您的 showeMenu: 方法不是您的操作所期望的签名。该操作正在寻找如下所示的方法签名:

- (void)showeMenu:(id)sender;

但是您已经在实现中指定了 IBAction 返回类型。

关于objective-c - 在导航栏上添加自定义 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8443383/

相关文章:

ios - 更改 UISwitch 图像

ios - 从数组中获取值

iphone - self.tableView insertRowsAtIndexPaths 来自 tableView 委托(delegate)

ios - Xcode Instruments 分配的总字节数太大

objective-c - 使用多个 NSUInteger 枚举作为方法的参数

ios - 不使用自动布局时,可以以编程方式使用 iOS 中的大小类/特征集合吗

ios - 突出显示动态创建的 UIButtons?

iphone UITableView 自定义单元格选定的图像

ios - 我在 TouchBegan 更新 CGVector 值时遇到问题

objective-c - Mac OS X 的命令行应用程序