打开按钮单击的新视图

时间:2013-04-22 08:57:52

标签: objective-c view

我有一个名为diffView的基于导航的应用程序,在其中我想打开按钮点击的新视图。为此,我创建了一个视图作为file-new file-cocoa touch class -UIViewControllerSubclass,命名为next1。

在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];
}

我没有收到任何错误,但当我点击按钮时没有任何反应。

请建议我解决。

2 个答案:

答案 0 :(得分:0)

我的想象力如何:

  1. 在你的委托中你加载 - 在初始化NavigationController时加载 - 初始化UINaviagationController并设置其rootViewController属性或仅使用initWithRootViewController:方法。这样的事情。

    viewController = [RootViewController alloc] init];
    nvc = [[UINavigationController alloc] initWithRootViewController:viewController];
    nvc.navigationBarHidden = YES;
    [self.window setRootViewController:nvc];
    [window makeKeyAndVisible];
    
  2. 在你的RootViewController中,从'viewDidLoad:'方法创建按钮并将其添加到'self.view'之后,你实现了目标选择器,一切都会好的。

  3. 编辑1:

    这是我的AppDelegate中的application:didFinishLaunchingWithOptions:方法

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

答案 1 :(得分:0)

是的我在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];
相关问题