0

我有一个名为 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];
}

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

请建议我任何解决方案。

4

2 回答 2

0

我如何想象它:

  1. 在您的委托中,您加载 - alloc/initUINaviagationController并设置其 rootViewController 属性,或者initWithRootViewController:在您初始化 NavigationController 时仅使用方法。类似的东西。

    viewController = [RootViewController alloc] init];
    nvc = [[UINavigationController alloc] initWithRootViewController:viewController];
    nvc.navigationBarHidden = YES;
    [self.window setRootViewController:nvc];
    [window makeKeyAndVisible];
    
  2. 在您的 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
于 2013-04-22T09:18:28.873 回答
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];
于 2013-04-23T10:26:37.640 回答