您的位置:首页 > 产品设计 > UI/UE

UI - presentViewControllerAndSingleton

2015-10-16 19:51 369 查看
<AppDelegate.m>

#import "AppDelegate.h"
#import "FirstViewController.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

FirstViewController *firstVC = [[FirstViewController alloc ]    init];
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:firstVC];
self.window.rootViewController = naVC;
[firstVC release];
[naVC release];

return YES;
}

- (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, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the 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. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


<FirstViewController.h>

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end


<FirstViewController.m>

#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ModalViewController.h"
#import "SingleHandle.h"
@interface FirstViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@end

@implementation FirstViewController

- (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.

//自定义NavigationBar
[self customNavigationBar];

//布局 imageView
[self layoutImageView];

}
//布局 imageView
-(void)layoutImageView
{
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 74, self.view.bounds.size.width - 20, self.view.bounds.size.height - 84)];
imageView.backgroundColor = [UIColor redColor];
imageView.tag = 100;
[self.view addSubview:imageView];
[imageView release];

//轻拍手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc ]initWithTarget:self action:@selector(presentPicker:)];
[imageView addGestureRecognizer:tap];
imageView.userInteractionEnabled = YES;
[tap release];

}

-(void)presentPicker:(UITapGestureRecognizer *)tap
{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
[self presentViewController:picker animated:YES completion:nil];
picker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

//设置代理
picker.delegate = self;
[picker release];
}
//picker delegate
//选中图片触发
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//获取 iamgeview  ,tag方法
UIImageView *imageView = (UIImageView *)[self.view viewWithTag:100];
//获取选取的图片
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

//给单例赋值
[SingleHandle shareSingleHandle].image = image;

//赋值
imageView.image = image;
//视图返回
[picker dismissViewControllerAnimated:YES completion:nil];

NSLog(@"%@",info);
}

//自定义NavigationBar
-(void)customNavigationBar
{
self.navigationItem.title = @"FirstVC";

//rightItem
//添加UIView的方法
UIButton *rightBt = [UIButton buttonWithType:UIButtonTypeSystem];
rightBt.frame = CGRectMake(0, 0, 80, 40);
[rightBt setTitle:@"push" forState:UIControlStateNormal];
[rightBt addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBt];
self.navigationItem.rightBarButtonItem = rightItem;
[rightItem release];

//leftItem
//Item 自定义 title 的方法
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"modal" style:UIBarButtonItemStylePlain target:self action:@selector(modal:)];

self.navigationItem.leftBarButtonItem = leftItem;
[leftItem release];

}

//模态
-(void)modal:(UIBarButtonItem *)item
{
// 创建对象
ModalViewController *modalVC = [[ModalViewController alloc]init];
//创建导航视图控制器来管理 modaVC   (不设置的话,其自身原本是不受 navigation 来管理的)
UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:modalVC];
//设置模态的样式
naVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
naVC.modalPresentationStyle = UIModalPresentationFullScreen;
//模态到下一个页面
[self presentViewController:naVC animated:YES completion:^{
NSLog(@"模态啦");
}];
[modalVC release];
[naVC release];

}

//push
-(void)push:(UIButton *)bt
{
SecondViewController *secondVC = [[SecondViewController alloc]init ];
[self.navigationController pushViewController:secondVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


<SecondViewController.h>

#import <UIKit/UIKit.h>
#import "FirstViewController.h"
@interface SecondViewController : UIViewController

@end


<SecondViewController.m>

#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (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.

self.view.backgroundColor = [UIColor greenColor];
self.navigationItem.title = @"SecondVC";

//rightItem
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"push" style:UIBarButtonItemStyleBordered target:self action:@selector(push:)];
self.navigationItem.rightBarButtonItem = rightItem;

}
//push 至thirdVC
-(void)push:(UIBarButtonItem *)rightItem
{
ThirdViewController *thirdVC = [[ThirdViewController alloc]init ];
[self.navigationController pushViewController:thirdVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


<ThirdViewController.h>

#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@end


<ThirdViewController.m>

#import "ThirdViewController.h"
#import "SingleHandle.h"
@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (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.

self.view.backgroundColor = [UIColor yellowColor];

UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
//从单例取值
imageView.image = [SingleHandle shareSingleHandle].image;

[self.view addSubview:imageView];
[imageView release];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


<ModalViewController.h>

#import <UIKit/UIKit.h>

@interface ModalViewController : UIViewController

@end


<ModalViewController.m>

#import "ModalViewController.h"

@interface ModalViewController ()

@end

@implementation ModalViewController

- (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.

self.view.backgroundColor = [UIColor cyanColor];

//自定义 navigationbar
[self customNavigationBar];

}
//自定义 navigationbar
-(void)customNavigationBar
{
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"dismiss" style:UIBarButtonItemStyleDone target:self action:@selector(dismiss:)];
self.navigationItem.rightBarButtonItem = rightItem;
[rightItem release];

}
-(void)dismiss:(UIBarButtonItem *)item
{
[self.navigationController dismissViewControllerAnimated:YES completion:^{
NSLog(@"回去了");
}];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end


<SigleHandle.h>

#import <UIKit/UIKit.h>
//单例就是只能创建一个对象的类
@interface SingleHandle : NSObject

@property (nonatomic,retain)UIImage *image;   //需要传什么值则添加什么属性

//单例创建方法一般以 share, stand, main 开头 + 当前类名
+(SingleHandle *)shareSingleHandle;

@end


<SigleHandle.m>

#import "SingleHandle.h"

@implementation SingleHandle

//声明静态变量
static SingleHandle *singlehanle = nil;

+(SingleHandle *)shareSingleHandle
{
//同步锁  //防止一种极限的可能,第一个对象正在创建的时候,第二个对象就开始创建了,造成两个对象
@synchronized(self){
if (singlehanle == nil) {
singlehanle = [[SingleHandle alloc]init];
}
return singlehanle;
}
}
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: