[iOS开发]多界面传值之代理传值

代理传值一般用于第二个页面传值给第一个页面
FirstViewController页面push到SecondViewController页面,如果SecondViewController页面的信息想回传到FirstViewController页面,用代理传值,其中SecondViewController定义协议和声明代理,FirstViewController确认并实现代理,FirstViewController作为SecondViewController的代理。

举例有两个View Controller,ViewControllerMine和ViewControllerPhotos,现在打算在ViewController Photo的照片墙上选择一张图片,并作为ViewControllerMine中一个头像按钮(buttonHeadImage)的背景图。
首先在ViewControllerPhotos中定义协议:

@protocol SelectPhotoDelegate <NSObject>

- (void) selectedPhoto: (UIImage*) headImage number: (NSInteger) numberOfPhotos;

@end

声明代理:

@property (nonatomic, assign) id<SelectPhotoDelegate> delegate;

在ViewController Mine中导入头文件并遵守协议,实现协议函数。

- (void)selectedPhoto:(UIImage *)headImage number:(NSInteger)numberOfPhotos {
    if (headImage) {
        [buttonHeadImage setBackgroundImage:headImage forState:UIControlStateNormal];
        [headerHead setBackgroundImage:headImage forState:UIControlStateNormal];
    }
    self->numberLabel.text = [NSString stringWithFormat:@"%ld",numberOfPhotos];
    self->numberLabel.textColor = [UIColor blackColor];
    [scrollViewMain addSubview:numberLabel];
}

设置代理

- (void)changeHeadImage:(UIButton*) headImageButton{
    ViewControllerPhotos* photoWall = [[ViewControllerPhotos alloc] init];
    //设置代理
    photoWall.delegate = self;
    UINavigationController* photoWallNavigationController = [[UINavigationController alloc] initWithRootViewController:photoWall];
    photoWallNavigationController.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:photoWallNavigationController animated:YES completion:nil];
}

这里传过来一个UIImage* 的参数,非空,则直接把头像按钮的背景改变,另一个参数代表选中图片张数。
再看ViewcontrollerPhotos文件:

//点按“确定”按钮
- (void)pressRightButton {
//创建警报控制器
    UIAlertController* confirmAlertController = [UIAlertController alertControllerWithTitle:@"选择头像" message:@"是否确认" preferredStyle:UIAlertControllerStyleAlert];
    //确认选中
    UIAlertAction* confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (self->imagesArray.count != 0) {
        //图片数组非空
            UIImage* headImage = self->imagesArray.firstObject;
            //执行代理函数,传参
            [self.delegate selectedPhoto:headImage number:self->imagesArray.count];
            [[NSUserDefaults standardUserDefaults] setObject: headImage.accessibilityIdentifier forKey:@"headImage"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        } else {
            [self.delegate selectedPhoto:nil number:0];
        }
        //选图视图dissmiss掉
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }];
    UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    	//点击了取消按钮,不进行响应
    }];
    [confirmAlertController addAction: cancel];
    [confirmAlertController addAction: confirm];
    [self presentViewController:confirmAlertController animated:YES completion:nil];
}

这样就把选图视图的图片参数与选图数量参数传到主视图中了。

属性传值

与代理传值相反,属性传值一般是正向的,第二个界面定义一个属性,在第一个界面直接访问,即可完成属性传值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值