5

Tôi có một lớp học UIViewController (Nói là XXX). Tôi trình bày bộ điều khiển chế độ xem này một cách khiêm tốn theo mã ..Làm thế nào để thêm UINavigationBar trong một UIViewController?

XXX *xxx = [ [XXX alloc] init]; 
[self presentModalViewController:xxx animated:YES]; 
[xxx release]; 

Tôi muốn thêm thanh điều hướng ở đầu chế độ xem XXX. Vì vậy, tôi đã sử dụng đối tượng UINavigationBar trong phương thức loadView của XXX.

UINavigationBar *navBar = [ [UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
[self.view addSubview:navBar]; 
[navBar release]; 

Nhưng, nó ném lỗi là "EXC_BAD_ACCESS". Có giúp gì không ...?

Cảm ơn

+0

Cố gắng thêm Navigationbar để XXX xem trong XIB –

+0

tôi không sử dụng IB đây – Confused

+0

Nếu u sử dụng Navigation hơn u phải đẩy nó ... tức là. 'pushViewController' đã được sử dụng thay thế hiện tạiModalViewController .. – Krunal

Trả lời

17

OPTION-1:

Hãy thử thêm thanh điều hướng từ XIB của viewController gọi là XXX.

OPTION-2:

Thêm một UINavigationController và trình bày nó modally.

Thay thế mã của bạn:

XXX *xxx = [[XXX alloc] init]; 
[self presentModalViewController:xxx animated:YES]; 
[xxx release]; 

với mã này:

XXX *xxx = [[XXX alloc] init]; 
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx]; 
[self presentModalViewController:navigation animated:YES]; 
[navigation release]; 

Hy vọng điều này sẽ giúp bạn.

+0

Cảm ơn .. Vì vậy, Chúng tôi không thể thêm UINavigationBar mà không tạo UINavigationController ở đây. Tôi có đúng không? – Confused

+0

@Rajkanth: Có, có lẽ đó phải là trường hợp như xa như tôi biết. Khi bạn đang nhận được một "EXC_BAD_EXCESS" khi bạn thử thêm nó theo chương trình. –

0

bạn làm điều đó như thế này:

XXX *xxx = [[XXX alloc] init]; 
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:xxx]; 
[self presentModalViewController:navigationController animated:YES]; 
[xxx release]; 
[navigationController release]; 
2

Thay thế mã của bạn với:

XXX *xxx = [[ [XXX alloc] init]autorelease]; 
    [self presentModalViewController:xxx animated:YES]; 

    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx]; 
    [self presentModalViewController:navigation animated:YES]; 
    [navigation release]; 

Tôi nghĩ rằng nó sẽ giải quyết của bạn "EXC_BAD_ACCESS" vấn đề.

1

bạn có thể thử điều này bằng cách thêm thanh công cụ ở đầu chế độ xem. Trong nhiều trường hợp tôi đã thấy cho poping MODAL điều khiển này là giải pháp tốt đẹp. nhưng nếu bạn muốn điều hướng nhiều bộ điều khiển từ bộ điều khiển MODAL thì bạn nên sử dụng UINavigationController.

enter image description here

Các vấn đề liên quan