支付宝

构架: App开发笔记-支付宝 如上图所示,1,创建Class文件夹
2.AppDelegate、Models、ViewController、
3,AppDelegate:AppDelegate.h、AppDelegate.m
4.ViewController:RootViewController、OtherViewControllers


下面就这个程序写几点心得:
1.创建根视图控制器
2.因为有四个主界面,所以声明四个导航控制器用来包含四个界面控制器

@interface JYFRootViewController ()

{

    // 建立四个导航控制器

    UINavigationController *_alipayNC;  // 支付宝

    UINavigationController *_serviceNC;   // 服务

    UINavigationController *_discoverNC;    // 探索

    UINavigationController *_wealthNC;  // 财富

}


@end

3.分别创建四个子视图控制器,并用导航控制器一 一包括,然后把导航控制器作为子视图控制器一 一添加到根视图控制器上

// 初始化子视图控制器

    

    JYFAlipayViewController *alipayVC = [JYFAlipayViewController new];

    _alipayNC = [[UINavigationController alloc] initWithRootViewController:alipayVC];

    [alipayVC release];

    [self addChildViewController:_alipayNC];

    

    JYFDiscoverTableViewController *discoverVC = [[JYFDiscoverTableViewController alloc]initWithStyle:UITableViewStyleGrouped];

    _discoverNC = [[UINavigationController alloc] initWithRootViewController:discoverVC];

    [discoverVC release];

    [self addChildViewController:_discoverNC];

    

    JYFServiceTableViewController *serviceVC = [[JYFServiceTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

    _serviceNC = [[UINavigationController alloc] initWithRootViewController:serviceVC];

    [serviceVC release];

    [self addChildViewController:_serviceNC];

    

    JYFWealthTableViewController *wealthVC = [[JYFWealthTableViewController alloc] initWithStyle:UITableViewStyleGrouped];

    _wealthNC = [[UINavigationController alloc] initWithRootViewController:wealthVC];

    [wealthVC release];

    [self addChildViewController:_wealthNC];

4.设置根视图控制器的View上将要显示的导航控制器的View,即默认View

 // 进行默认view的显示

    [self.view addSubview:_alipayNC.view];

5.添加分段控制器用来分别控制四个导航控制器

 NSArray *itemArray = @[[UIImage imageNamed:@"model0.png"],

                           [UIImage imageNamed:@"model1.png"],

                           [UIImage imageNamed:@"model2.png"],

                           [UIImage imageNamed:@"model3.png"]];

    UISegmentedControl *segmentedControl = [[UISegmentedControl allocinitWithItems:itemArray];

    segmentedControl.backgroundColor = [UIColor clearColor];

    segmentedControl.frame = CGRectMake(0CGRectGetHeight(self.view.frame) - 49self.view.frame.size.width49);

    [segmentedControl addTarget:selfaction:@selector(segmentedControlAction:)forControlEvents:UIControlEventValueChanged];

    segmentedControl.tag = 123;

    [self.view addSubview:segmentedControl];

 

    [segmentedControl release];

}


#pragma mark - 实现方法

- (void)segmentedControlAction:(UISegmentedControl *)sender

{

    // 切换需要显示的控制器

    // 移除当前显示的视图

    for (UIView *view in self.view.subviews) {

        if (![view isKindOfClass:[UISegmentedControl class]]) {

            [view removeFromSuperview];

        }

    }

    // 判断, 添加view

    switch (sender.selectedSegmentIndex) {

        case 0: { // 显示_alipayVC.view

            [self.view insertSubview:_alipayNC.view atIndex:0];

            break;

        }

        case 1: { // 显示_serviceVC.view

            [self.view insertSubview:_serviceNC.view atIndex:0];

            break;

        }

        case 2: { // 显示_discoverVC.view

            [self.view insertSubview:_discoverNC.view atIndex:0];

            break;

        }

        case 3: { // 显示_wealthVC.view

            [self.view insertSubview:_wealthNC.view atIndex:0];

            break;

        }

            

        default:

            break;

    }

}

6.因为要通过按钮来进行页面跳转,跳转后的页面上可能不需要UISegmentedControl,所以要在根视图控制器中设置隐藏和显示分段控制器的方法
首先在.h文件中声明方法

#import 


@interface JYFRootViewController : UIViewController


#pragma mark - 声明方法

- (void)hiddenSegmentedControl;

- (void)showSegmentedControl;


@end


然后在.m文件中实现方法

#pragma mark - 隐藏分段控制器的方法的实现

- (void)hiddenSegmentedControl

{

    [self.view viewWithTag:123].hidden = YES;

}

#pragma mark - 显示分段控制器的方法的实现

- (void)showSegmentedControl

{

    [self.view viewWithTag:123].hidden = NO;

}

7.主界面上的按钮过多,要通过点击按钮来进行页面跳转,如何才能不用if或者swich语句来进行判断进而进行跳转以及传值操作?
循环添加所有按钮,每个按钮都是一张图片,我们先把所有按钮的点击之后将要跳转的控制器的名称用一个数组进行封装
首先自定义添加按钮的方法

#pragma mark - 创建Button

- (UIButton *)createButtonWithFrame:(CGRect)frame

                    backgroundImage:(UIImage *)bgImage

                              title:(NSString *)title

                                tag:(NSInteger)tag

                           selector:(SEL)sel

{

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

    button.frame = frame;

    [button setBackgroundImage:bgImage forState:UIControlStateNormal];

    [button setTitle:title forState:UIControlStateDisabled];

    button.tag = tag;

    button.showsTouchWhenHighlighte= YES;

    [button addTarget:self action:selforControlEvents:UIControlEventTouchUpInside];

    return button;

}


 // 控制器名称对应按钮的表示用一个数组来保存

    NSArray *buttonVCNameArray = @[

                                   @"BFASSaoYiSaoViewController"//  扫一扫

                                   @""// 付款码

                                   @""// 余额宝

                                   @""// 转账

                                   @""// 信用卡还款

                                   @""// 当面付

                                   @""// 手机充值

                                   @""// 淘宝

                                   @""// 手机宝令

                                   @""// 我的快递

                                   @""// AA收款

                                   @""// 记账本

                                   @""// 收款

                                      // 注意这里是控制器的名称

                                   @"JYFMoreViewController"  // 更多

                                   ];

循环添加按钮的方法

// 循环添加12个Button

    CGFloat x = 0, y = 100, width = 80, height = 95;

    for (int i = 0; i < 12; i++) {

        

        CGRect frame = CGRectMake(x, y, width, height);

        // 创建Button

        UIButton *button = [self createButtonWithFrame:frame

                                       backgroundImage:[UIImage                           imageNamed:@"yuebao_btn_bg.png"];


                                    // 注意这里的title是通过数组来添加的

                                                 title:buttonVCNameArray[i + 2]

                                                   tag:kBaseTag + 3 + i

                                              selector:@selector(buttonAction:)];

        [scrollView addSubview:button];

        

        // 修改下次循环的位置

        x += 80;

        if (x >= 320) {

            x = 0;

            y += height;

        }

    }

}

因为这些按钮本视图上的,所以要在本视图上进行点击,然后才能向控制器传递信息,所以要在本视图上创建协议,以实现代理传值
1.声明协议及方法

#import


@class JYFALipayView;

@protocol JYFAlipayViewDelegate <<span style="color: #703daa">NSObject>

// 传值的方法

- (void)alipayView:(JYFALipayView *)view

  didSelectAtIndex:(NSInteger)index withTitle:(NSString *)title;


@end

2.声明代理属性

@interface JYFALipayView : UIView

@property (nonatomic, assign) id<</span>JYFAlipayViewDelegate> delegate;


@end

3.去点击事件中通过代理去调用协议中声明的方法从而获取按钮信息

#pragma mark - 点击事件

- (void)buttonAction:(UIButton *)sender

{

    // 给代理发送协议

    // 1.先判断

    if (_delegate != nil && [_delegaterespondsToSelector:@selector(alipayView:didSelectAtIndex:withTitle:)]) {

        [_delegate alipayView:self didSelectAtIndex:sender.tag - kBaseTagwithTitle:[sender titleForState:UIControlStateDisabled]];

    }

}

4.来到控制器遵守视图中声明的协议以及方法

@interface JYFAlipayViewController ()<</span>JYFAlipayViewDelegate>

5.设置背视图控制器作为视图的代理以接收视图穿过来的按钮的信息以便处理跳转事件

  // 设置代理

    _alipayView.delegate = self;

6.实现协议中的方法

#pragma mark - 实现协议方法

- (void)alipayView:(JYFALipayView *)view didSelectAtIndex:(NSInteger)index withTitle:(NSString *)title

{

    NSLog(@"%@", title);

    // 隐藏分段控制器

    JYFRootViewController *rootVC =(JYFRootViewController *)self.parentViewController.parentViewController;

    // 调用方法进行隐藏

    [rootVC hiddenSegmentedControl];


   // 根据控制器名称创建出来对象

    Class c = NSClassFromString(title);

    // 根据c创建视图控制器

    UIViewController *vc = [[c allocinit];

    // 跳转视图控制器

    [self.navigationController pushViewController:vc animated:YES];

    [vc release];

}

7.上面在跳转下一个视图控制器之后把分段控制器按钮给隐藏了,所以要在视图将要跳转回来(即视图即将显示)的时候将隐藏的分段控制器控件给显示出来

#pragma mark 视图将要显示的时候显示分段控制器

- (void)viewWillAppear:(BOOL)animated

{

    // 创建将要显示视图的视图控制器对象

    JYFRootViewController *rootVC =(JYFRootViewController *)self.parentViewController.parentViewController;

    // 调用JYFRootViewController已经写好的显示分段控制器的方法对其视图上的分段控制器重新显示出来

    [rootVC showSegmentedControl];

 

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值