Tag Archives: iPhone

[iPhone] UIImageView 사용법

간단한걸 적기 왠지 쑥쓰럽지만 UIImage를 이용한 이미지를 뷰에 붙이기 위해 사용하는 UIImageView를 사용하는 방법입니다.

UIView < UIImageView < UIImage 순으로 붙인다는 것을 먼저 이해하시면 되겠네요.

[code]UIImage *icon = [UIImage imageNamed:@”image.png”];
UIImageView *imageView *[UIImageView alloc] initWithImage:icon];
imageView.frame = CGRectMake(5,5,15,15);[/code]
위와 같은 방법으로 리소스에 등록된 image.png파일을 5 x 5 위치에 붙일 수 있습니다.

참고 : http://discussions.apple.com/thread.jspa?messageID=7852897

[iPhone] UITabBarController 이용시 UIViewController의 viewDidLoad가 호출되지 않는 문제 해결

UIViewController를 초기화 할때는 보통 다음과 같은 방식을 사용합니다.
[code]UIViewController *viewController = [[UIViewController alloc] init];[/code]
혹은 NIB를 이용한 다음과 같은 방법도 사용하게 됩니다.
[code]UIViewController *viewController = [[UIViewController alloc] initWithNibName:@”nibName” bundle:[NSBundle mainBundle]];[/code]
위와 같이 호출할 경우에는 자동으로 초기화가 되며 loadView, viewDidLoad가 순차적으로 호출되게 됩니다.

그런데 UITabBarController를 Interface Builder를 사용하여 다른 UIViewController를 등록하여 사용하게 되면 위의 두 메서드가 초기화시에 호출되지 않습니다.

또한 비슷한 컨트롤인 UINavigationController로 같은 문제를 가지고 있습니다. 이게 버그인지 무엇인지 잘 모르겠군요.

이런 난감한 문제를 해결하기 위해서 검색을 해보았지만 역시나 IB를 사용하지 않고 초기화 하는 방법밖에 없는 모양입니다.

[code]// UITabBarController 초기화
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.customizableViewControllers = nil;
 
// 3개의 다른 UIViewController 생성
// 3개의 뷰는 UINavigationView를 사용합니다.
UIViewController *View1 = [[UIViewController alloc] init];
UINavigationController *tab1Controller = [[UINavigationController alloc] initWithRootViewController:View1];
tab1Controller.tabBarItem.image = [UIImage imageNamed:@”1.png”];
[View1 release];
 
UIViewController *View2 = [[UIViewController alloc] init];
UINavigationController *tab2Controller = [[UINavigationController alloc] initWithRootViewController:View2];
tab2Controller.tabBarItem.image = [UIImage imageNamed:@”2.png”];
[View2 release];
 
UIViewController *View3 = [[UIViewController alloc] init];
UINavigationController *tab3Controller = [[UINavigationController alloc] initWithRootViewController:View3];
tab3Controller.tabBarItem.image = [UIImage imageNamed:@”3.png”];
[View3 release];
 
tabBarController.viewControllers = [NSArray arrayWithObjects: tab1Controller, tab2Controller, tab3Controller, nil];
 
[window addSubview: tabBarController.view];[/code]
위와 같은 방법을 사용하시면 정상적으로 작동하는 것을 알 수 있습니다.

참고  : http://discussions.apple.com/thread.jspa?threadID=1670615