【问题标题】:navigate to new page on button click in iPhone在 iPhone 中单击按钮导航到新页面
【发布时间】:2012-06-16 11:01:25
【问题描述】:

我是 iPhone 开发者的新手。

我想用动画导航到新页面,但我无法导航到新页面,

这是我的代码 sn-p,

UIButton *Planbtn = (UIButton *)[self.view viewWithTag:1];
    [Planbtn addTarget:self action:@selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];

..

-(void)btnClicked{
    NSLog(@"btnClicked");

    PlanPage *yourViewController = [[PlanPage alloc] initWithNibName:@"PlanPage" bundle:[NSBundle mainBundle]];
    [self.navigationController pushViewController:yourViewController animated:TRUE];
    [yourViewController release];
}

我可以在我的日志中看到btnClicked

提前致谢!

【问题讨论】:

  • 您需要将 btnClicked 链接到您的界面事件出口
  • 发布更多细节,您是否也在实施UINavigationController
  • 如果你看到 NSLog,我想你链接了你的 iBoutlet。所以也许你的 navigationController 是 nil 因为没有初始化。
  • 查看我的编辑,我在我的项目中只添加了这么多代码,我还需要添加什么吗?
  • 重要的是要知道你的rootViewController是否有导航控制器。您可以拥有一个有或没有它的应用程序,因此您的应用程序很可能不是基于导航的应用程序

标签: iphone animation navigation uibutton buttonclick


【解决方案1】:

将这些属性写入AppDelegate.h 文件中

@property (strong, nonatomic) YourFirstViewController *yourFirstController;
@property (nonatomic, retain) UINavigationController *navigationControl;

将此代码写入AppDelegate.m文件中

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {

        self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
        self.yourFirstViewController = [[YourFirstViewController alloc] initWithNibName:@"YourFirstViewController" bundle:nil]];
        navigationController = [[UINavigationController alloc] initWithRootViewController:self.yourFirstViewController];
        [self.window addSubview:[navigationController view]];
        [self.window makeKeyAndVisible];
        return YES;
    }

我认为这对你会有帮助。

【讨论】:

    【解决方案2】:

    基于你的应用视图,你不能推送控制器,最简单的做法是:

    PlanPage *yourViewController = [[PlanPage alloc] initWithNibName:@"PlanPage" bundle:[NSBundle mainBundle]];
    [self.view addSubView:yourViewController.view];
    [yourViewController release];
    

    如果你想要动画,恐怕你要么自己实现动画,要么添加一个导航控制器来为你完成这项工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 1970-01-01
      • 2021-08-19
      • 2023-03-28
      • 2019-12-28
      • 2022-01-04
      • 1970-01-01
      相关资源
      最近更新 更多