2

私は周りを検索しましたが、それを理解できないようです。多くの人が私やそのようなものへのリンクを持っていると確信しています。しかし、誰かが私に次のことをするためのコードを見せてくれたら、

UINavigationBar「戻る」として左矢印を入れたいのですがUINavigationItem。これどうやってするの?これが私の現在のコードですUIViewController

theBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:nil action:@selector(backButtonSelected:)];

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Options" style:UIBarButtonItemStyleBordered target:nil action:@selector(optionsButtonSelected:)];

UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:@"Title"];
item.leftBarButtonItem = leftButton;
item.hidesBackButton = YES;
item.rightBarButtonItem = rightButton;

[self.view addSubview:theBar];
4

2 に答える 2

2

これはあなたが探しているものかもしれないと思います。

// in .h file
@property (nonatomic, retain) UINavigationBar *navBar;

// in .m file just below @implementation
@synthesize navBar;


// within .m method
navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 48.0f)];
navBar.barStyle = UIBarStyleBlack;

UINavigationItem *title = [[UINavigationItem alloc] initWithTitle:@"Nav Bar Title"];

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] 
                       initWithTitle:@"Back" 
                               style:UIBarButtonItemStylePlain 
                              target:nil
                              action:@selector(backButtonSelected:)];

title.leftBarButtonItem = leftButton; 

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] 
         initWithBarButtonSystemItem:UIBarButtonSystemItemAction
                              target:nil
                              action:@selector(showActionSheet:)];

title.rightBarButtonItem = rightButton;

[navBar pushNavigationItem:title animated:YES];

[self.view addSubview:navBar];
于 2012-07-10T23:29:30.653 に答える
1

UINavigationController画面をポップ/プッシュするには、を使用する必要がありUIViewControllersます。ナビゲーションコントローラーは自動的にを追加するUINavigationBarのでUIViewControllers、作成したように作成する必要はありません。

これがサンプルです。私はメモリリークを探しませんでした。アプリデリゲートには、次の方法があります。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{        
    // Override point for customization after application launch.
    MainVC *mainVC = [[MainVC alloc] init];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:mainVC];
    [self.window addSubview:navController.view];
    [self.window makeKeyAndVisible];

    return YES;
}

MainVCはUIViewController、階層のレベル1を表すです。その中に私は持っています

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"MainVC";

    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"SecondLevel" style:UIBarButtonItemStyleBordered target:self action:@selector(secondLevelSelected:)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

- (void) secondLevelSelected:(id)sender
{
    SecondVC *secondVC = [[SecondVC alloc]  init];
    [self.navigationController pushViewController:secondVC animated:YES];
}

SecondVCはUIViewController、階層の2番目のレベルを表すもう1つのものです。ここに、必要な戻るボタンがあります。

- (void)loadView
{
    self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 240)];
    self.view.backgroundColor = [UIColor greenColor];
    self.title = @"SecondVC";

    self.navigationItem.hidesBackButton = YES;
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"FirstLevel" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBtnSelected:)];
    self.navigationItem.leftBarButtonItem = leftBtn;
}

- (void) leftBtnSelected:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
于 2011-01-04T17:24:28.677 に答える