ios Delegate 实现 视图之间通信

MyDelete.h

objectivec 复制代码
#import <Foundation/Foundation.h>

@protocol MyDelegate <NSObject>
- (void)didUpdateText:(NSString *)text; // 委托方法
@end

ViewControler.h

objectivec 复制代码
//
//  ViewController.h
//  delegate
//
//  Created by xmkjsoft on 2024/8/12.
//

#import <UIKit/UIKit.h>
#import "MyDelegate.h"

@interface ViewController : UIViewController<MyDelegate>


@end

ViewControler.m

objectivec 复制代码
//
//  ViewController.m
//  delegate
//
//  Created by xmkjsoft on 2024/8/12.
//

#import "ViewController.h"
#import "OtherViewController.h"


@interface ViewController ()


@property (nonatomic, strong) UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    [self loadUI];
      

    
    // 初始化 FirstViewController 并设置代理
    OtherViewController *firstVC = [[OtherViewController alloc] init];
    firstVC.delegate = self; // 设置代理
    
  
}

-(void) loadUI{
    // Set red background color
    self.view.backgroundColor = [UIColor redColor];
    
    // 创建标签以显示接收到的文本
       self.label = [[UILabel alloc] initWithFrame:CGRectMake(50, 200, 300, 50)];
       self.label.textAlignment = NSTextAlignmentCenter;
       [self.view addSubview:self.label];
    
    // 创建按钮以跳转到 OtherViewController
       UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
       [button setTitle:@"打开新视图" forState:UIControlStateNormal];
       button.frame = CGRectMake(100, 300, 200, 50); // 设置按钮的 frame
       [button addTarget:self action:@selector(showOtherViewController) forControlEvents:UIControlEventTouchUpInside];
       [self.view addSubview:button];
}


- (void)showOtherViewController {
    OtherViewController *otherVC = [[OtherViewController alloc] init];
    otherVC.delegate = self; // 设置代理
    [self presentViewController:otherVC animated:YES completion:nil];
}



// 实现代理方法
- (void)didUpdateText:(NSString *)text {
    self.label.text = text; // 更新标签文本
}



@end

OtherViewController.h

objectivec 复制代码
//
//  OtherViewController.h
//  delegate
//
//  Created by xmkjsoft on 2024/8/12.
//

#import <UIKit/UIKit.h>

#import "MyDelegate.h"

NS_ASSUME_NONNULL_BEGIN


@interface OtherViewController : UIViewController
@property (nonatomic, weak) id<MyDelegate> delegate; // 代理属性

@end

NS_ASSUME_NONNULL_END

OtherViewController.m

objectivec 复制代码
//
//  OtherViewController.m
//  delegate
//
//  Created by xmkjsoft on 2024/8/12.
//

#import "OtherViewController.h"
#import "ViewController.h"

@interface OtherViewController ()

@property (nonatomic, strong) UIPanGestureRecognizer *panGestureRecognizer;

@end

@implementation OtherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 添加手势识别器
       self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
       [self.view addGestureRecognizer:self.panGestureRecognizer];

    
    [self loadUI];

    
    
}


- (void)handlePan:(UIPanGestureRecognizer *)gesture {
    CGPoint translation = [gesture translationInView:self.view];
    
    if (gesture.state == UIGestureRecognizerStateChanged) {
        // 检测滑动方向
        if (translation.y < -30) {
            NSLog(@"向上滑动");
            // 在这里处理向上滑动的逻辑
        } else if (translation.y > 30) {
            NSLog(@"向下滑动");
            // 在这里处理向下滑动的逻辑
            if ([self.delegate respondsToSelector:@selector(didUpdateText:)]) {
                               [self.delegate didUpdateText:@"上个视图通过下滑让我变hello,hello"];
                           }
            
            [self closeModal];
        }
    }
}


- (void)closeModal {
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"模态视图已关闭");
    }];
}


- (void) loadUI{
    self.view.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    UIButton *getOtherViewControllerButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [getOtherViewControllerButton setTitle:@"点我更新上一个视图内容" forState:UIControlStateNormal];
    [getOtherViewControllerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [getOtherViewControllerButton setBackgroundColor:[UIColor blueColor]];
    [getOtherViewControllerButton.layer setCornerRadius:5.0];
    getOtherViewControllerButton.frame = CGRectMake(100, 100, 200, 50);
    [getOtherViewControllerButton addTarget:self action:@selector(getOtherViewControllerMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:getOtherViewControllerButton];
    
    
    // 创建标签并设置文本
        UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, 350, 50)]; // 设置标签位置
        textLabel.textAlignment = NSTextAlignmentCenter; // 文本居中
        textLabel.textColor = [UIColor whiteColor]; // 设置文本颜色
        textLabel.text = @"下滑用代理更新上一个视图的文本为hello"; // 默认文本
        // 将标签添加到视图中
        [self.view addSubview:textLabel];
}



- (void)getOtherViewControllerMethod {


    if ([self.delegate respondsToSelector:@selector(didUpdateText:)]) {
                       [self.delegate didUpdateText:@"我是通过代理从别的视图里触发显示的"];
                   }
    
}


@end
相关推荐
dnekmihfbnmv2 小时前
好用的电容笔有哪些推荐一下?年度最值得推荐五款电容笔分享!
ios·电脑·ipad·平板
黑果魏叔2 小时前
macOS Sequoia 正式版(24A335)黑苹果/Mac/虚拟机系统镜像
macos
tekin3 小时前
macos macport软件包管理工具 sudo port install xxx 安装的软件的路径 与 brew install xxx 软件安装路径总结
macos·brew·port·macport·port install·port软件包安装路径·brew软件包安装路径
Hellc0075 小时前
MacOS升级ruby版本
前端·macos·ruby
GEEKVIP12 小时前
Android 恢复挑战和解决方案:如何从 Android 设备恢复删除的文件
android·笔记·安全·macos·智能手机·电脑·笔记本电脑
逢生博客17 小时前
Mac 搭建仓颉语言开发环境(Cangjie SDK)
macos·华为·鸿蒙
Rverdoser20 小时前
MacOS Catalina 从源码构建Qt6.2开发库之01: 编译Qt6.2源代码
macos
Magnetic_h20 小时前
【iOS】单例模式
笔记·学习·ui·ios·单例模式·objective-c
归辞...1 天前
「iOS」——单例模式
ios·单例模式·cocoa
GEEKVIP1 天前
如何在没有备份的情况下恢复 Mac 上丢失的数据
经验分享·笔记·安全·macos·电脑·笔记本电脑·改行学it