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

第一步  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];
    
}

 

 

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

转载于:https://www.cnblogs.com/gzy0724/p/3852618.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值