'아이폰 Navigation'에 해당되는 글 1건

  1. 2012.07.22 아이폰 Navigation Controller 만들기 예제 따라하기
C/Objective C/ios2012.07.22 13:03

- 아이폰의  Navigation Controller 를 이용해서 View 전환 즉 화면 전환을 해보도록 하자.


- 화면 디자인


- Navigation Controller 만들기 순서

1. Xcode 실행

2. Utility Application
3. Project Name : “UtilityTest”
4. storyboard의 첫 번째 xib 파일에 Label 위치
5. Assistant Editor로 Label 드래그
6. “myLabel” 입력
7. @interface 라인 끝에서 “{” 엔터
8. “NSString *dispStr;”
9. storyboard의 두 번째 xib 파일에 Switch 위치
10. Assistant Editor로 Switch 드래그
11. “mySw” 입력
12. Assistant Editor로 Switch 드래그 – Connection을 “Action”으로 변경 

13. “changeSW” 입력
14. @interface 라인 끝에서 “{” 엔터
15. “NSString *myStr;” 입력
16. } 아래에 “@property(strong, nonatomi) NSString *myStr;” 입력
17. MainViewController.m 파일 편집

- (void)viewDidLoad
{ [super viewDidLoad];

dispStr = @"YES";

myLabel.text = dispStr; }

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller { dispStr = controller.myStr;

myLabel.text = dispStr;

[self dismissModalViewControllerAnimated:YES]; }

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([[segue identifier] isEqualToString:@"showAlternate"]) {

[[segue destinationViewController] setDelegate:self];

FlipsideViewController *viewController = (FlipsideViewController *)[segue destinationViewController];

viewController.myStr = dispStr; }

}

18. FlipsideViewController.m 파일 편집

@synthesize myStr; ...
- (void)viewDidLoad

{

[super viewDidLoad];

if (myStr == @"YES") { _mySw.on = YES;

} else {
_mySw.on = NO;

} }

- (IBAction)done:(id)sender
{ [self.delegate flipsideViewControllerDidFinish:self]; }- (IBAction)changeSW:(id)sender{

if (_mySw.on) { myStr = @"YES";

} else {
myStr = @"NO";

} } 


- MainViewController.h  소스코드

#import "FlipsideViewController.h"


@interface MainViewController : UIViewController <FlipsideViewControllerDelegate, UIPopoverControllerDelegate>

{

NSString *dispStr;

}


@property (strong, nonatomic) UIPopoverController *flipsidePopoverController;


@property (weak, nonatomic) IBOutlet UILabel *myLabel;


@end


- MainViewController.h 소스코드

#import "MainViewController.h"


@interface MainViewController ()


@end


@implementation MainViewController


@synthesize flipsidePopoverController = _flipsidePopoverController;

@synthesize myLabel = _myLabel;


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

dispStr = @"YES";

_myLabel.text = dispStr;

}


- (void)viewDidUnload

{

    [self setMyLabel:nil];

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

} else {

    return YES;

}

}


#pragma mark - Flipside View Controller


- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller

{

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

        [self dismissModalViewControllerAnimated:YES];

    } else {

        [self.flipsidePopoverController dismissPopoverAnimated:YES];

        self.flipsidePopoverController = nil;

    }

dispStr = controller.myStr;

_myLabel.text = dispStr;

[self dismissModalViewControllerAnimated:YES];

}


- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController

{

    self.flipsidePopoverController = nil;

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    if ([[segue identifier] isEqualToString:@"showAlternate"]) {

        [[segue destinationViewController] setDelegate:self];

FlipsideViewController *viewController = (FlipsideViewController *)[segue destinationViewController];

viewController.myStr = dispStr;

        

        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {

            UIPopoverController *popoverController = [(UIStoryboardPopoverSegue *)segue popoverController];

            self.flipsidePopoverController = popoverController;

            popoverController.delegate = self;

        }

    }

}


- (IBAction)togglePopover:(id)sender

{

    if (self.flipsidePopoverController) {

        [self.flipsidePopoverController dismissPopoverAnimated:YES];

        self.flipsidePopoverController = nil;

    } else {

        [self performSegueWithIdentifier:@"showAlternate" sender:sender];

    }

}


@end


- FlipsideViewController.h 소스코드

#import <UIKit/UIKit.h>


@class FlipsideViewController;


@protocol FlipsideViewControllerDelegate

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller;

@end


@interface FlipsideViewController : UIViewController{

NSString *myStr;

}

@property (strong, nonatomic) NSString *myStr;

@property (weak, nonatomic) id <FlipsideViewControllerDelegate> delegate;


- (IBAction)done:(id)sender;


@property (weak, nonatomic) IBOutlet UISwitch *mySw;

- (IBAction)changeSW:(id)sender;


@end



- FlipsideViewController.m 소스코드

#import "FlipsideViewController.h"


@interface FlipsideViewController ()


@end


@implementation FlipsideViewController

@synthesize myStr;

@synthesize mySw = _mySw;


@synthesize delegate = _delegate;


- (void)awakeFromNib

{

self.contentSizeForViewInPopover = CGSizeMake(320.0, 480.0);

    [super awakeFromNib];

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

if (myStr == @"YES") { _mySw.on = YES;

} else {

_mySw.on = NO;

}

}


- (void)viewDidUnload

{

    [self setMySw:nil];

    [super viewDidUnload];

    // Release any retained subviews of the main view.

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

} else {

    return YES;

}

}


#pragma mark - Actions


- (IBAction)done:(id)sender

{

    [self.delegate flipsideViewControllerDidFinish:self];

}


- (IBAction)changeSW:(id)sender {

if (_mySw.on) { myStr = @"YES";

} else {

myStr = @"NO";

}

}

@end


- 실행화면
     






신고
Posted by Namseungil