IOS-项目总结(一)

一、页面之间的跳转、传递参数

 1.1  通过代码创建控制器:

首先创建控制器AViewController和BViewController,点击控制器A中的button跳转到B控制器传值
,B控制器有个name属性,在跳转的方法里传值

-(void)btnClick{
BViewController *BVC = [[BViewController alloc] init];
BVC.name = @"要传的值";
[self.navigationController pushViewController:BVC animated:YES ];
}

1.2  、通知

在A控制器发送通知

//需要传的参数
NSDictionary *dict = @{@"key":value};
//发送通知
[[NSNotificationCenter defaultCenter]postNotificationName:@"sendDataToTwoVc" object:nil userInfo:dict];

在B控制器监听通知

//监听通知(通知名字一定要写正确)
 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setData:) name:@"sendDataToTwoVc" object:nil];
//监听通知后调用
-(void)setData:(NSNotification *)notification{
     NSLog(@"dict - %@",notification.userInfo);
}
//移除需要观察的通知
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
//注意:如果发送的通知指定了object对象,那么观察者接收的通知设置的object对象与其一样,才会接收到通知,但是接收通知如果将这个参数设置为了nil,则会接收一切通知。

1.3 、NSUserDefault

//设置需要保存的对象
[[NSUserDefaults standardUserDefaults] setObject:@"value" forKey:@"key"];
//同步磁盘
[[NSUserDefaults standardUserDefaults] synchronize];
//获取保存的对象
[[NSUserDefaults standardUserDefaults]objectForKey:@"key"];

2、控制器之间的逆向传值:

2.1、block
有两个控制器分别是AViewController和BViewController,点击A控制器中的按钮跳转到B控制器,点击B控制器的屏幕回到A控制器,并把B控制器textField.text传到A控制器中,改变A控制器中btn的title.

//A控制器.m文件
#import "AViewController.h"
#import "BViewController.h"

@implementation AViewController
//button的点击事件
- (IBAction)clickDown:(id)sender {
    BViewController *vc = [[BViewController alloc] init];
    vc.backBlock = ^(NSString *titleStr) {
            [self.btn setTitle:titleStr forState:UIControlStateNormal];
        };
    [self.navigationController pushViewController:vc animated:YES];
}
//B控制器.h文件
#import <UIKit/UIKit.h>

@interface BViewController : UIViewController
@property(nonatomic,copy)void(^backBlock)(NSString *titleStr);
@end

//B控制器.m文件
#import "BViewController.h"

@interface BViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField;
@end

@implementation BViewController

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    self.backBlock(self.textField.text);
}

2.2、代理
在B控制器中指定协议,让A控制器遵守协议设置A控制器为代理并实现协议中的方法

A控制器.m文件
#import "AViewController.h"
#import "BViewController.h"

@interface AViewController ()<BViewControllerDelegate>

@end

@implementation AViewController

- (IBAction)clickDown:(id)sender {
//在使用block和代理时注意循环引用问题,当一个对象持有block,而该block又持有该对象时,类似下面的伪代码会照成循环引用,__weak typeof(self) weakself=self;
    __weak typeof(self) weakSelf = self;
    BViewController *vc = [[BViewController alloc] init];
    vc.delegate = weakSelf;
    [self.navigationController pushViewController:vc animated:YES];
}
//协议中的方法:
-(void)sendString:(NSString *)str{
//B控制器传过来的值str
    [self.btn setTitle:str forState:UIControlStateNormal];
}
//A控制器.h文件
#import <UIKit/UIKit.h>

@protocol BViewControllerDelegate <NSObject>
-(void)sendString:(NSString *)str;
@end
@interface BViewController : UIViewController
@property(weak,nonatomic)id<BViewControllerDelegate>delegate;
@end

//A控制器.m文件
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.delegate sendString:self.textField.text];
    [self.navigationController popViewControllerAnimated:YES];
}



作者:星星爱上月亮
链接:https://www.jianshu.com/p/84f6a1ea2e00
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

二、调试

https://www.cnblogs.com/daiweilai/p/4421340.html

三、JS与IOS的交互

JS交互实现流程

如果用WKWebView,JS调iOS端必须使用window.webkit.messageHandlers.kJS_Name.postMessage(null),跟调安卓的不一样,kJS_Name是iOS端提供的JS交互name,在注入JS交互Handler时用到:[userContentController addScriptMessageHandler:self name:kJS_Name]

下面有个HTML端的iOSCallJsAlert函数,里面会执行alert弹窗,并通过JS调iOS端(kJS_Name)

function iOSCallJsAlert() {
        alert('弹个窗,再调用iOS端的kJS_Name');
        window.webkit.messageHandlers.kJS_Name.postMessage({body: 'paramters'});
}

实现在iOS端通过JS调用这个iOSCallJsAlert函数,并接受JS调iOS端的ScriptMessage。有以下主要代码:

首先添加JS交互的消息处理者(遵守WKScriptMessageHandler协议)以及JS_Name(一般由iOS端提供给Web端)。

[WKUserContentController addScriptMessageHandler:JS_ScriptMessageReceiver name:JS_Name]

有添加就有移除,一般在ViewDidDisappear中移除,不然JS_ScriptMessageReceiver会被强引用而无法释放(内存泄露),个人猜测是被WebKit里面某个单例强引用。

[userContentController removeScriptMessageHandlerForName:JS_Name]

实现WKScriptMessageHandler协议方法,用来接收JS调iOS的消息。
WKScriptMessage.name即[WKUserContentController addScriptMessageHandler:JS_ScriptMessageReceiver name:JS_Name]中的JS_Name,可以区分不同的JS交互,message.body是传递的参数。

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
    JKLog(@"JS调iOS  name : %@    body : %@",message.name,message.body);
}

iOS端调JS中的函数就简单多了,调用一个方法即可。
@"iOSCallJsAlert()"代表要调用的函数名,如果有参数就这样写@"iOSCallJsAlert('p1','p2')"

[webView evaluateJavaScript:@"iOSCallJsAlert()" completionHandler:nil]

作者:溪枫狼
链接:https://www.jianshu.com/p/e65137ced997
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

参考:https://www.jianshu.com/p/e65137ced997

https://www.jianshu.com/p/687536a160a7

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值