码迷,mamicode.com
首页 > 其他好文 > 详细

初学者如何新建一个封装的类来封装方法

时间:2014-07-18 14:31:38      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:blog   color   文件   io   for   cti   

第一步  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];
    
    ViewController1 *viewCtrl1 = [[ViewController1 alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:viewCtrl1];
    navi.navigationBarHidden = YES;
    self.window.rootViewController = navi;
    
    return YES;
}


第二步   设置一个方法Function,并让他继承UIViewController  然后在.m文件中实现这些方法,并在.h中声明

//封装按钮
- (UIButton *)gzybutton:(NSString *)image andFrame:(CGRect)frame0 andAction:(SEL)action0
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image1 = [UIImage imageNamed:image];
    [btn setImage:image1 forState:UIControlStateNormal];
    [btn addTarget:self action:action0 forControlEvents:UIControlEventTouchUpInside];
    btn.frame = frame0;
    [self.view addSubview:btn];
    return btn;
}

//封装视图
- (UIImageView *)gzyView :(NSString *)image0 andFram :(CGRect)fram
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:fram];
    UIImage *image = [UIImage imageNamed:image0];
    [imageView setImage:image];
    [self.view addSubview:imageView];
    return imageView;
}

//封装所有日文按钮
- (UIButton *) japanNextFram :(CGRect)fram andAction :(SEL)action
{
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn3.frame = fram;
    //    btn3.backgroundColor = [UIColor clearColor];
    btn3.backgroundColor = [UIColor clearColor];
    [btn3 addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
    return btn3;
}


//广告按钮的封装
- (UIButton *) creatAdBtn :(NSString *)title andFram :(CGRect)fram andAct :(SEL)act
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
    btn.frame = fram;
    btn.backgroundColor = [UIColor grayColor];
    [btn addTarget:self action:act forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    return btn;
}

 

后面新建UIViewController的子类的时候直接继承function即可

#import "Function.h"

@interface ViewController1 : Function

@end


.m文件中的代码就变得很简单了 
- (void) creatView
{
    [self gzyView:@"main_Bg" andFram:CGRectMake(0, 0, 320, 580)];
}

- (void) creatBtn
{
    [self gzybutton:@"main_study" andFrame:CGRectMake(10, 400, 93, 163)  andAction:@selector(nextStep)];
}

新建一个push跳转到下个类的对象
- (void) nextStep
{
    ViewController2 *viewCtrl2 = [[ViewController2 alloc] init];
    [self.navigationController pushViewController:viewCtrl2 animated:YES];
}


这是pop返回上个类的对象

- (void) back
{
    [self.navigationController popViewControllerAnimated:YES];
}


这个按钮方法代表跳转到模态视图


- (void) jumpAd :(UIButton *)sender
{
    AdViewController *adViewCtrl = [[AdViewController alloc] init];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:adViewCtrl];
    navi.navigationBarHidden = NO;
//    [self presentModalViewController:navi animated:YES];
    [self presentViewController:navi animated:YES completion:nil];
    
}

 

 

以上对初学者应该较有帮助

初学者如何新建一个封装的类来封装方法,布布扣,bubuko.com

初学者如何新建一个封装的类来封装方法

标签:blog   color   文件   io   for   cti   

原文地址:http://www.cnblogs.com/gzy0724/p/3852618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!