日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS万能跳转界面的方法

發(fā)布時(shí)間:2024/4/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS万能跳转界面的方法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

在開發(fā)項(xiàng)目中,會(huì)有這樣變態(tài)的需求: 推送:根據(jù)服務(wù)端推送過來的數(shù)據(jù)規(guī)則,跳轉(zhuǎn)到對(duì)應(yīng)的控制器 feeds列表:不同類似的cell,可能跳轉(zhuǎn)不同的控制器(噓!產(chǎn)品經(jīng)理是這樣要求:我也不確定會(huì)跳轉(zhuǎn)哪個(gè)界面哦,可能是這個(gè)又可能是那個(gè),能給我做靈活嗎?根據(jù)后臺(tái)返回規(guī)則任意跳轉(zhuǎn)?) 思考:wocao!這變態(tài)的需求,要拒絕他嗎? switch判斷唄,考慮所有跳轉(zhuǎn)的因素?這不得寫死我...switch () {case :break;default:break; } 我是這么個(gè)實(shí)現(xiàn)的(runtime是個(gè)好東西) 利用runtime動(dòng)態(tài)生成對(duì)象、屬性、方法這特性,我們可以先跟服務(wù)端商量好,定義跳轉(zhuǎn)規(guī)則,比如要跳轉(zhuǎn)到A控制器,需要傳屬性id、type,那么服務(wù)端返回字典給我,里面有控制器名,兩個(gè)屬性名跟屬性值,客戶端就可以根據(jù)控制器名生成對(duì)象,再用kvc給對(duì)象賦值,這樣就搞定了 ---O(∩_∩)O哈哈哈比如:根據(jù)推送規(guī)則跳轉(zhuǎn)對(duì)應(yīng)界面HSFeedsViewControllerHSFeedsViewController.h:進(jìn)入該界面需要傳的屬性 @interface HSFeedsViewController : UIViewController// 注:根據(jù)下面的兩個(gè)屬性,可以從服務(wù)器獲取對(duì)應(yīng)的頻道列表數(shù)據(jù)/** 頻道ID */ @property (nonatomic, copy) NSString *ID;/** 頻道type */ @property (nonatomic, copy) NSString *type;@end AppDelegate.m:推送過來的消息規(guī)則 // 這個(gè)規(guī)則肯定事先跟服務(wù)端溝通好,跳轉(zhuǎn)對(duì)應(yīng)的界面需要對(duì)應(yīng)的參數(shù) NSDictionary *userInfo = @{@"class": @"HSFeedsViewController",@"property": @{@"ID": @"123",@"type": @"12"}}; 接收推送消息 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {[self push:userInfo]; } 跳轉(zhuǎn)界面 - (void)push:(NSDictionary *)params {// 類名NSString *class =[NSString stringWithFormat:@"%@", params[@"class"]];const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding];// 從一個(gè)字串返回一個(gè)類Class newClass = objc_getClass(className);if (!newClass){// 創(chuàng)建一個(gè)類Class superClass = [NSObject class];newClass = objc_allocateClassPair(superClass, className, 0);// 注冊(cè)你創(chuàng)建的這個(gè)類 objc_registerClassPair(newClass);}// 創(chuàng)建對(duì)象id instance = [[newClass alloc] init];// 對(duì)該對(duì)象賦值屬性NSDictionary * propertys = params[@"property"];[propertys enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {// 檢測(cè)這個(gè)對(duì)象是否存在該屬性if ([self checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {// 利用kvc賦值 [instance setValue:obj forKey:key];}}];// 獲取導(dǎo)航控制器UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;UINavigationController *pushClassStance = (UINavigationController *)tabVC.viewControllers[tabVC.selectedIndex];// 跳轉(zhuǎn)到對(duì)應(yīng)的控制器 [pushClassStance pushViewController:instance animated:YES]; } 檢測(cè)對(duì)象是否存在該屬性 - (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName {unsigned int outCount, i;// 獲取對(duì)象里的屬性列表objc_property_t * properties = class_copyPropertyList([instanceclass], &outCount);for (i = 0; i < outCount; i++) {objc_property_t property =properties[i];// 屬性名轉(zhuǎn)成字符串NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];// 判斷該屬性是否存在if ([propertyName isEqualToString:verifyPropertyName]) {free(properties);return YES;}}free(properties);return NO; }文/漢斯哈哈哈(簡(jiǎn)書作者) 原文鏈接:http://www.jianshu.com/p/8b3a9155468d 著作權(quán)歸作者所有,轉(zhuǎn)載請(qǐng)聯(lián)系作者獲得授權(quán),并標(biāo)注“簡(jiǎn)書作者”。

?

轉(zhuǎn)載于:https://www.cnblogs.com/LzwBlog/p/6080725.html

總結(jié)

以上是生活随笔為你收集整理的iOS万能跳转界面的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。