2011-12-09 76 views
0

这是我的代码。在导航栏上添加CustomView

- (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 :)。

+1

你确定它没有被调用?您的origin.x为720,并且不在iPhone边界内。所以也许你的弹出窗口在那里,但不可见 –

+0

它在Ipad中,第二件事是我也通过调试来检查它,当我按下它时它不会调用“showMenue”方法。 –

+0

把一个NSLog放入你的'showMenue'(为什么不是'showMenu'- - ),检查该方法是否被调用。 – Kjuly

回答

3

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

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

从技术上讲,如果指定niltarget参数,响应链中搜索响应行动的对象。您遇到的问题可能是由于您的showeMenu:方法不是您的操作期望的签名。该行动正在寻找看起来像这样的方法签名:

- (void)showeMenu:(id)sender; 

然而,你指定在执行IBAction返回类型。

+0

感谢StuDev!我已检查,但它确实有所作为,仍然不起作用... –

+0

先生!我已经完成了所有建议的解决方案,但没有人以正确的方式工作。您可以在Xcode中确认后上传您的建议解决方案。 –

+0

感谢“Zaki Shaheen”bhai!问题在于初始化帧大小.........感谢所有试图帮助我的人。 –