打开按钮点击新视图
我有一个名为diffView的导航为基础的应用程序,并在其中我想打开新的按钮点击视图。为此,创建了一个名为next1的文件新文件 - 可可触摸类UIViewControllerSubclass。打开按钮点击新视图
在diffViewAppDelegate.h我写的
IBOutlet UIButton *btn1;
@property (nonatomic,retain) UIButton *btn1;
-(IBAction)buttonPressed:(id)sender;
和diffViewAppDelegate.m-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect bounds=window.bounds;
UIView* view=[[UIView alloc]initWithFrame:bounds];
[view setBackgroundColor:[UIColor redColor]];
[window addSubview:view];
CGRect buttonFrame= CGRectMake(80, 240, 200, 30);
btn1=[[UIButton alloc] initWithFrame:buttonFrame];
[btn1 setTitle:@"space" forState:UIControlStateNormal];
[btn1.titleLabel setFont:[UIFont fontWithName:@"Verdana" size:20]];
[btn1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[view addSubview:btn1];
[self.window makeKeyAndVisible];
return YES;
}
-(IBAction)buttonPressed:(id)sender
{
next1 * nxt=[[next1 alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nxt animated:YES];
[nxt release];
}
我没有得到任何错误,但是当我按一下按钮没有任何反应。
请给我任何解决方案。
我如何把它想象:
-
在你的委托加载 - 分配/初始化的
UINaviagationController
并设置其属性RootViewController的或只是使用initWithRootViewController:
方法,当你初始化的NavigationController。类似的东西。viewController = [RootViewController alloc] init]; nvc = [[UINavigationController alloc] initWithRootViewController:viewController]; nvc.navigationBarHidden = YES; [self.window setRootViewController:nvc]; [window makeKeyAndVisible];
在你RootViewController的,从“viewDidLoad中:”你的方法创建按钮,并把它添加到以后您实现目标选择,一切都会好起来的“self.view”。
编辑1:
这是我application:didFinishLaunchingWithOptions:
方法在我的AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] init] autorelease];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
这是我的RootViewController的
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect buttonFrame= CGRectMake(80, 240, 200, 30);
UIButton *btn1=[[UIButton alloc] initWithFrame:buttonFrame];
btn1.backgroundColor = [UIColor greenColor];
[btn1 setTitle:@"space" forState:UIControlStateNormal];
[btn1.titleLabel setFont:[UIFont fontWithName:@"Verdana" size:20]];
[btn1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)buttonPressed:(id)sender
{
NSLog(@"sender: %@", sender);
}
@end
是的我已经创建了RootViewController中的按钮,但它不工作。即使按钮现在不可见。我的代码是这样的 - – 2013-04-23 10:20:06
CGRect buttonFrame = CGRectMake(80,240,200,30); \t btn1 = [[UIButton alloc] initWithFrame:buttonFrame]; \t [btn1 setTitle:@“space”forState:UIControlStateNormal]; \t [btn1.titleLabel setFont:[UIFont fontWithName:@“Verdana”size:20]]; \t [btn1 addTarget:self action:@selector(buttonPressed :) forControlEvents:UIControlEventTouchDown]; \t [view1 addSubview:btn1]; – 2013-04-23 10:23:48
发布更新,给出的示例正在工作。 – h4cky 2013-04-23 11:09:23
是的,我已经在RootViewController的,但其产生的按钮不工作。即使按钮现在不可见。我的代码是这样的 -
CGRect buttonFrame= CGRectMake(80, 240, 200, 30);
btn1=[[UIButton alloc] initWithFrame:buttonFrame];
[btn1 setTitle:@"space" forState:UIControlStateNormal];
[btn1.titleLabel setFont:[UIFont fontWithName:@"Verdana" size:20]];
[btn1 addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn1];
尝试将UIControlEventTouchDown更改为UIControlEventTouchUpInside。 – Girish 2013-04-22 09:01:56