开发者

Buttons doesn't work

I have UINavigationBar with buttons than have been created using custom view. I added a target with selector for each buttom, but buttons have not respond to selector. Can you help me?

- (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 for开发者_如何学运维State: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];
}


Sorry Sveta,

I didn't see your complete code.Check this out.It will work for you.

- (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");
}

In Appdelegate add this code

- (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;
}


I never did it programmatically.

Could you post your changeType?

It might looks like

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

and should be declared in the interface section;

Also you could put a breakpoint on it to see if it called at all. And see the value of the variables. Please check if it called.

EDIT:

Below my code that you can insert into defaultly created Navigation-Based Application:

at 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...");
}

at RootViewController.h:

-(void)buttonPressed:(id)sender;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜