상세 컨텐츠

본문 제목

[iOS 개발] IBAction을 사용하여 View 전환하기

헉!!/iOS

by 권태성 2011. 7. 22. 22:29

본문


IBAction을 사용하여 버튼 터치를 통해 View간의 전환을 위한 소스입니다.


AppDelegate.h 소스
─────────


#import <UIKit/UIKit.h>


@interface ChangeViewAppDelegate : NSObject <UIApplicationDelegate> {

    UIWindow *window;

IBOutlet UIView *firstView;

IBOutlet UIView *secondView;

}


-(IBAction) ChangeView;


@property (nonatomic, retain) IBOutlet UIWindow *window;


@end




AppDelegate.m 소스
─────────


#import "ChangeViewAppDelegate.h"


@implementation ChangeViewAppDelegate


@synthesize window;



#pragma mark -

#pragma mark Application lifecycle


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

    //어플리케이션이 런칭되고나서 실행되는 메소드


//어플을 처음 실행하면 나오는 화면이 윈도우인데 윈도우에view 넣을  있게 해줌.

[window addSubview:firstView];

[self.window makeKeyAndVisible];

    

    return YES;

}


- (IBAction) ChangeView

{

//UIwindow 붙일 있는 View 하나이므로 처음에 UIwindow View1 넣었다가 다른 뷰로 보여주기 위해서 View1 없애고 View2 보여주는 방식으로 구현해야함

NSArray *subs = [window subviews];

//윈도우에서 subview 제거

[[subs objectAtIndex:0] removeFromSuperview];

//objectAtIndex secondView 경우에는

if([subs objectAtIndex:0] == secondView)

{

//window firstView 보여줌

[window addSubview:firstView];

}

else

{

//secondView 아닐경우 secondView 보여줌

[window addSubview:secondView];

}


}



- (void)applicationWillResignActive:(UIApplication *)application {

    /*

     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

     */

}



- (void)applicationDidEnterBackground:(UIApplication *)application {

    /*

     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 

     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.

     */

}



- (void)applicationWillEnterForeground:(UIApplication *)application {

    /*

     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.

     */

}



- (void)applicationDidBecomeActive:(UIApplication *)application {

    /*

     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

     */

}



- (void)applicationWillTerminate:(UIApplication *)application {

    /*

     Called when the application is about to terminate.

     See also applicationDidEnterBackground:.

     */

}



#pragma mark -

#pragma mark Memory management


- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {

    /*

     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.

     */

}



- (void)dealloc {

    [window release];

    [super dealloc];

}



@end


─────────



귀찮아서 메소드 정리를 안하고 그냥 올립니다.

인터페이스 빌더에서는 mainwindow.xib을 열어 view를 두개 만들고 App Delegate와 연결한후

각 뷰에 버튼을 하나씩 넣고 버튼을 다시 App Delegate의 Received Actions에 연결해주면 됩니다.

다른 버전은 모르겠는데 Xcode 3.2에서

- (IBAction) ChangeView

{

}
 

과 같은 형태로 선언시에  
-(IBAction) ChangeView 처럼 -과 (사이를 붙이면 인터페이스 빌더에서

Received Actions 항목이 안뜨니 참고하세요.



 

관련글 더보기