用storyboard添加一个导航栏,其中首页有一个switch,与setting联动,还有一个button,使用modal连接另一个viewControl,其上也有一个按钮,按下销毁本viewControl,回到前一页。
实现步骤:
1.创建一个SingleView的项目,勾选上storyboard。
2.向storyboard中添加一个NavigationController,两个ViewController,然后在NavigationController中右击指向第一个ViewController,然后设置为rootViewController,并且将箭头指向NavigationController。
3.将两个ViewController的Class分别设置为DXWViewController和DXWViewController1(两个自己创建的类,继承自ViewController)
4.创建setting文件,并将root.plist保存一个键值对,key改成switch
5.修改DXWViewController(主视图)
DXWViewController.h:
#import <UIKit/UIKit.h> #import "DXWViewController1.h" @interface DXWViewController : UIViewController<DXWFlipsideViewControllerDelegate> - (IBAction)change:(id)sender; @property (retain, nonatomic) IBOutlet UILabel *label; @property (retain, nonatomic) IBOutlet UISwitch *switchButton; - (IBAction)showInfo:(id)sender; @end
DXWViewController.m:
#import "DXWViewController.h" @interface DXWViewController () @end @implementation DXWViewController -(void)viewWillAppear:(BOOL)animated { [self changeData]; } -(void)changeData { NSUserDefaults *usr = [NSUserDefaults standardUserDefaults]; NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@YES,@"switch", nil]; [usr registerDefaults:dic]; ((UILabel *)self.label).text = [usr boolForKey:@"switch"]?@"开":@"关"; self.switchButton.on = [usr boolForKey:@"switch"]; //都要写入一下 [usr synchronize]; } - (void)viewDidLoad { [super viewDidLoad]; UIApplication *app = [UIApplication sharedApplication]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeData) name:UIApplicationWillEnterForegroundNotification object:app]; } - (void)dealloc { [self.switchButton release]; [_label release]; [super dealloc]; } //实现协议的方法 - (void)flipsideViewControllerDidFinish:(DXWViewController1 *)controller { [self dismissViewControllerAnimated:YES completion:nil]; } - (IBAction)change:(id)sender { UISwitch *switchButton = sender; NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; [user setBool:switchButton.on forKey:@"switch"]; [user synchronize]; ((UILabel *)self.label).text = [user boolForKey:@"switch"]?@"开":@"关"; } - (IBAction)showInfo:(id)sender { UIStoryboard *strBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; DXWViewController1 *controller = [strBoard instantiateViewControllerWithIdentifier:@"DXWViewController1"]; controller.delegate = self; controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; [self presentViewController:controller animated:YES completion:nil]; } @end
上图中showInfo方法是通过代码的方法实现跳转到下一个view,如果是通过storyboard实现连线的方法然后跳过下一个view是这样实现:
@“Add”是连线的ID
//连线的方法 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { if ([segue.identifier isEqualToString:@"Add"]) { DXWViewController1 *controlller = segue.destinationViewController; controlller.delegate = self; } }
6.修改DXWViewController1(子视图)
DXWViewController.h:
#import <UIKit/UIKit.h> @class DXWViewController1; @protocol DXWFlipsideViewControllerDelegate - (void)flipsideViewControllerDidFinish:(DXWViewController1 *)controller; @end @interface DXWViewController1 : UIViewController @property (assign, nonatomic) id <DXWFlipsideViewControllerDelegate> delegate; - (IBAction)done:(id)sender; @end
DXWViewController.m:
#import "DXWViewController1.h" @interface DXWViewController1 () @end @implementation DXWViewController1 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)done:(id)sender { [self.delegate flipsideViewControllerDidFinish:self]; } @end