Iphone 按钮不';行不通
我有Iphone 按钮不';行不通,iphone,objective-c,button,uibarbuttonitem,uinavigationcontroller,Iphone,Objective C,Button,Uibarbuttonitem,Uinavigationcontroller,我有UINavigationBar和按钮,而不是使用自定义视图创建的按钮。我为每个按钮添加了一个带有选择器的目标,但按钮并没有响应选择器。你能帮我吗 - (void)viewDidLoad { [super viewDidLoad]; [self.navigationItem setTitle:@"Title"]; // create custom view container = [[UIView alloc] init]; [container se
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];
}
对不起,斯维塔
我没有看到你的完整代码。请检查此代码。它对你有用
- (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;
}
我从来没有用编程的方式做过 你能把你的更改类型贴出来吗 可能看起来像
-(void)changeType:(id)sender {
// doing something here
}
并应在接口部分声明
您还可以在其上放置一个断点,以查看是否调用了它。并查看变量的值。请检查它是否打过电话
编辑:
下面是可以插入默认创建的基于导航的应用程序的“我的代码”:
在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;
[[UIButton alloc]init]的样式不好-请改用工厂方法[UIButton buttonWithStyle:…]。在这种情况下,最好使用:
[[UIButton alloc]initWithCustomView:]代码>如何创建按钮无关紧要,我还没有找到Style:和initWithCustomView:的方法buttonWithStyle:。有以下按钮:类型:和init:,initWithFrame:,和initWithCoder:。在更新的答案中,我没有集中精力讨论frame。很抱歉上次的答案是:(