2011-10-07 113 views
0

我有UINavigationBar與使用自定義視圖創建的按鈕。我爲每個按鈕添加了一個選擇器,但按鈕沒有響應選擇器。你可以幫我嗎?按鈕不起作用

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self.navigationItem setTitle:@"Title"]; 

    // create custom view 
    container = [[UIView alloc] init]; 
    [container setUserInteractionEnabled:TRUE]; 

    NSString *path; 
    // create 2 buttons and put in a custom view 
    buttonList = [[UIButton alloc] init]; 
    path = [[NSBundle mainBundle] pathForResource:@"dotActive" ofType:@"png"]; 
    UIImage *imgList = [[[UIImage alloc] initWithContentsOfFile:path] autorelease]; 
    path = [[NSBundle mainBundle] pathForResource:@"dotInactive" ofType:@"png"]; 
    UIImage *imgListPrs = [[[UIImage alloc] initWithContentsOfFile:path] autorelease]; 
    [buttonList setImage:imgListPrs forState:UIControlStateSelected]; 
    [buttonList setSelected:TRUE]; 
    [buttonList setFrame:CGRectMake(-30, 0, imgList.size.width, imgList.size.height)]; 
    [buttonList setImage:imgList forState:UIControlStateNormal]; 
    [container addSubview:buttonList]; 
    [buttonList addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside]; 
    [buttonList setUserInteractionEnabled:TRUE]; 


    buttonPic = [[UIButton alloc] init]; 
    path = [[NSBundle mainBundle] pathForResource:@"dotActive" ofType:@"png"]; 
    UIImage *imgPic = [[[UIImage alloc] initWithContentsOfFile:path] autorelease]; 
    path = [[NSBundle mainBundle] pathForResource:@"dotInactive" ofType:@"png"]; 
    UIImage *imgPicPrs = [[[UIImage alloc] initWithContentsOfFile:path] autorelease]; 
    [buttonPic setImage:imgPicPrs forState:UIControlStateSelected]; 
    [buttonPic setFrame:CGRectMake(-10, 0, imgPic.size.width, imgPic.size.height)]; 
    [buttonPic setImage:imgPic forState:UIControlStateNormal]; 
    [container addSubview:buttonPic]; 
    [buttonPic addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside]; 
    [buttonPic setUserInteractionEnabled:TRUE]; 

    // create UIBarButtonItem with a custom view 
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container]; 

    // put item on navigation bar 
    [self.navigationItem setRightBarButtonItem:item]; 
} 
+1

[[UIButton alloc] init]是不好的樣式 - 使用工廠方法[UIButton buttonWithStyle :. ..]代替。 – Eiko

+0

那麼最好在這種情況下使用:'[[UIButton alloc] initWithCustomView:];' – Nekto

+0

如何創建按鈕並沒有找到方法buttonWithStyle:和initWithCustomView :.有buttonWithType:和init :, initWithFrame:和initWithCoder :. – Sveta

回答

1

對不起斯韋塔卡特,

我沒看到你們全部code.Check這out.It會爲你工作。

- (void)viewDidLoad{ 

[super viewDidLoad]; 

[self.navigationItem setTitle:@"Title"]; 

    // create custom view 
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 30)]; 
[container setUserInteractionEnabled:TRUE]; 

    // create 2 buttons and put in a custom view 
UIButton *buttonList = [[UIButton alloc] init]; 
[buttonList setSelected:TRUE]; 
[buttonList setBackgroundColor:[UIColor redColor]]; 

[buttonList setFrame:CGRectMake(0, 0, 30.0, 30.0)]; 
[container addSubview:buttonList]; 
[buttonList addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside]; 
[buttonList setUserInteractionEnabled:TRUE]; 
[buttonList release]; 


UIButton *buttonPic = [[UIButton alloc] init]; 
[buttonPic setFrame:CGRectMake(40.0, 0, 30.0, 30.0)]; 
[buttonPic setBackgroundColor:[UIColor blueColor]]; 
[container addSubview:buttonPic]; 
[buttonPic addTarget:self action:@selector(changeType:) forControlEvents:UIControlEventTouchUpInside]; 
[buttonPic setUserInteractionEnabled:TRUE]; 
[buttonPic release]; 

    // create UIBarButtonItem with a custom view 
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container]; 

    // put item on navigation bar 
[self.navigationItem setRightBarButtonItem:item]; 
} 

- (void)changeType:(id)sender 
{ 
    NSLog(@"Change Type"); 
} 

在AppDelegate中添加以下代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
// Override point for customization after application launch. 

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; 
    self.window.rootViewController = navController; 
    [self.window makeKeyAndVisible]; 
    [navController release]; 
    return YES; 
} 
+0

它不能解決我的問題 – Sveta

+0

我沒有專注於此更新的答案中的幀。請留下最後的答案。 :( –

+0

它也行不通 – Sveta

0

我從來沒那麼編程。

你能發佈你的changeType嗎?

它可能看起來像

-(void)changeType:(id)sender { 
// doing something here 
} 

,應在接口部分聲明;

你也可以在它上面放一個斷點,看看它是否打電話。並查看變量的值。請檢查它是否打電話。

編輯:

下面我的代碼,你可以插入defaultly創建基於導航應用:

在RootViewController.m:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight]; 
    [infoButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    UIBarButtonItem *tmpInfoButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton]; 
    self.navigationItem.rightBarButtonItem = tmpInfoButton; 
    [tmpInfoButton release]; 

} 

-(void)buttonPressed:(id)sender { 
    NSLog(@"Button pressed..."); 
} 

在RootViewController.h:

-(void)buttonPressed:(id)sender;