如何简单地为测试切换 App ios appdelegatee

当前位置:
如何在Appdelegate中通过获取keyWindow来present一个UIViewAlertViewController
如何在Appdelegate中通过获取keyWindow来present一个UIViewAlertViewController
来源:网络整理&&&&&时间: 1:24:26&&&&&关键词:
关于网友提出的“如何在Appdelegate中通过获取keyWindow来present一个UIViewAlertViewController”问题疑问,本网通过在网上对“如何在Appdelegate中通过获取keyWindow来present一个UIViewAlertViewController”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:如何在Appdelegate中通过获取keyWindow来present一个UIViewAlertViewController
描述:1.问题描述我想要在- (BOOL)application:(UIApplication )application didFinishLaunchingWithOptions:(NSDictionary )launchOptions中弹出present出一个UIViewAlertViewController,效果就是在启动页的时候弹出更新提示,但是因为当前的这个window还没有成为keyWindow,所以在启动页消失后,层次图就会变成下面这样。是否只能等首页出现弹出UIAlertViewController才能正确的在keyWindow上弹出?2.代码[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertControler animated:YES completion:^{
也使用过UIWindow *window = [UIApplication sharedApplication].delegate.
[window makeKeyAndVisible];
[window.rootViewController presentViewController:alertControler animated:YES completion:^{
3.请教1.怎么样才是正确的姿势。。。2.是否是因为当前的keyWindow和启动图过后出现首页时的keyWindow不同。(我觉得是这个原因)解决方案1:为什么一定要用keywindow呢?用导航控制器或者tabBarController不可以么?解决方案2:用导航推不行吗?解决方案3:为何不测试一下呢,打印下它们的地址,看看是不是同一个?解决方案4:根据你的需求,你应该在启动页里操作,而不是入口类
以上介绍了“如何在Appdelegate中通过获取keyWindow来present一个UIViewAlertViewController”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1146046.html
上一篇: 下一篇:如何取到appdelegate的变量_百度知道
如何取到appdelegate的变量
答题抽奖
首次认真答题后
即可获得3次抽奖机会,100%中奖。
j88r知道合伙人
IT类认证行家
知道合伙人
采纳数:32494
获赞数:21316
从事多年系统运维,喜欢编写各种小程序和脚本。
方案有很多种:1, 通过AppDelegate保存为全局变量,再获取2,使用NSUSerDefault第一种 :通过AppDelegate方法:定义全局变量////
AppDelegate.h////
Created by MISSAJJ on 15/5/5.//
Copyright (c) 2015年 MISSAJJ. All rights reserved.//#import &UIKit/UIKit.h&@interface AppDelegate : UIResponder &UIApplicationDelegate &@property (strong, nonatomic) UIWindow *@property (strong, nonatomic) NSString *globalAppThemeC@property (strong, nonatomic) NSString *globalAboutT @end在AppDelegate.m 内赋值:_globalAppThemeColor = appDetails.appThemeC_globalAboutTag = appDetails.在需要的VC头部导入#import &AppDelegate.h&- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate * appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate]; }
获得变量NSString *about = appDelegate.globalAboutTNSString *theme = appDelegate.globalAppThemeC灵活运用到代码需求的地方//navi设置为全局主题色self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:appDelegate.globalAppThemeColor alpha:1];=================I am 华丽丽的分割线 ^_^========================第二种 :通过NSUserDefaults方法:查找了相关资料,自己整理了一份NSUserDefaults文摘给有共同有需求的程序猿朋友们.一
,NSUserDefaults 简单的运用方法NSUserDefaults一般可以存取一些短小的信息,比如存入再读出一个字符串到NSUserDefaults注意 : key值必须要相同才能读取出来哦! NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary,
不是所有数据都能往里放滴哦~//存储数据NSString *string = [NSString stringWithString @&我想存储的字符串内容&];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:string forKey:@&theme&]; //在存储数据的地方,别忘了这一句 [[NSUserDefaults standardUserDefaults] synchronize];//在需要的地方获取数据NSString *getStringValue
= [[NSUserDefaults standardUserDefaults] objectForKey:@&theme&];二 , 如果需要保存比较多得数据, 可以通过模型保存和读取1.模型代码////
AreasModel.h//////
Created by MISSAJJ on 15/5/7.//
Copyright (c) 2015年 MISSAJJ. All rights reserved.//#import &Foundation/Foundation.h&@interface AreasModel : NSObject/* * about 模型 */@property (nonatomic,copy)NSString *@property (nonatomic,copy)NSString * appThemeC@end记住必须要在 M 文件 里 写这两个方法- (id) initWithCoder: (NSCoder *)coder- (void) encodeWithCoder: (NSCoder *)coder然后把该自定义的类对象编码到 NSData中,再从NSUserDefaults中进行读取。////
AreasModel.m//////
Created by MISSAJJ on 15/5/7.//
Copyright (c) 2015年 MISSAJJ. All rights reserved.//#import &AreasModel.h&@implementation AreasModel- (id)initWithCoder:(NSCoder *)aDecoder{
if(self = [super init])
self.about
= [aDecoder decodeObjectForKey:@&about&];
self.appThemeColor
= [aDecoder decodeObjectForKey:@&appThemeColor&];
}}- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.about forKey:@&about&];
[aCoder encodeObject:self.appThemeColor forKey:@&appThemeColor&];
}@end2. 存储数据的代码//////////////////////////
以上省略........////////////////////////// //URL编码成UTF8
dirPath = [dirPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * dirUrl = [NSURL URLWithString:dirPath];
NSMutableURLRequest * dirRequest = [NSMutableURLRequest requestWithURL:dirUrl];
NSData *dirJsonData = [NSURLConnection sendSynchronousRequest:dirRequest returningResponse:nil error:nil];
NSDictionary *dirListJsonData = [NSJSONSerialization JSONObjectWithData:dirJsonData options:0 error:nil];
NSDictionary* dicData = [dirListJsonData objectForKey:@&data&];
#pragma mark ====保存临时主题和关于信息====
NSString *about = [dicData objectForKey:@&about&];
NSString *theme = [dicData objectForKey:@&theme&];
//创建模型
AreasModel *themeAndAbout = [[AreasModel alloc] init];
themeAndAbout.about =
themeAndAbout.appThemeColor =
//保存数据,用归档保存到NSUserDefault
NSData *themeAndAboutData = [NSKeyedArchiver archivedDataWithRootObject:themeAndAbout];
[[NSUserDefaults standardUserDefaults] setObject:themeAndAboutData forKey:@&themeAndAbout&];
[[NSUserDefaults standardUserDefaults] synchronize];3. 获取数据代码 //获得保存数据
NSData *getthemeAndAboutData = [[NSUserDefaults standardUserDefaults] objectForKey:@&themeAndAbout&]; //转成模型获取数据
AreasModel *getThemeAndAbout =
[NSKeyedUnarchiver unarchiveObjectWithData:getthemeAndAboutData];
NSLog(@&%@,%@&,getThemeAndAbout.appThemeColor, getThemeAndAbout.about);
为你推荐:
其他类似问题
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。使用category 为 AppDelegate 的代码分层
Category是一种为现有的类添加新方法的方式。
利用Objective-C的动态运行时分配机制,Category提供了一种比继承(inheritance)更为简洁的方法来对class进行扩展,无需创建对象类的子类就能为现有的类添加新方法,可以为任何已经存在的class添加方法。
AppDelegate 是iOS app启动的入口,经常需要添加各类的第三方应用,比如:定位服务、统计服务、消息推送服务、支付服务等。
各类服务的代码如果完全放在一个文件中,给和修改带来极大的不便。
利用category的特点,可以将不同的服务分别保存在不同的category(类)文件中,让代码更加简洁,结构层次更加分明,阅读和修改容易。
二、原始文件
1、AppDelegate.h
#import &uikit uikit.h=&&&
@interface AppDelegate : UIResponder &uiapplicationdelegate&
@property (strong, nonatomic) UIWindow *
&/uiapplicationdelegate&&/uikit&
2、AppDelegate.m
#import &AppDelegate.h&
@interface AppDelegate ()
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
return YES;
- (void)applicationWillResignActive:(UIApplication *)application {
- (void)applicationDidEnterBackground:(UIApplication *)application {
- (void)applicationWillEnterForeground:(UIApplication *)application {
- (void)applicationDidBecomeActive:(UIApplication *)application {
- (void)applicationWillTerminate:(UIApplication *)application {
三、创建AppDelegate的category
1、选中 AppDelegate.h
2、同时按住 command + shift + s ,将AppDelegate.h 另存为
3、名称 AppDelegate+WeixinPayService.h
4、同样操作,修改AppDelegate.m
5、将名称改为AppDelegate+WeixinPayService.m
6、打开 AppDelegate+WeixinPayService.h 文件,修改
#import &AppDelegate.h&
#import &WXApi.h&
@interface AppDelegate (WeixinPayService)
-(void)initWeixnPayService:(UIApplication *)application WithOption:(NSDictionary *)launchO
7、打开AppDelegate+WeixinPayService.m,修改
#import &AppDelegate+WeixinPayService.h&
@implementation AppDelegate (WeixinPayService)
-(void)initWeixnPayService:(UIApplication *)application WithOption:(NSDictionary *)launchOptions
[WXApi registerApp:@&xxxxxxxxxxxxxxxxxxxxxxxxx&];
8、导入 weixinPayService到 AppDelegate.m中
#import &AppDelegate+WeixinPayService.h&
9、修改AppDelegate.m 的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self initWeixnPayService:application WithOption:launchOptions];
return YES;
10、在需要回调的其他方法中,以同样的方式回调WeixinPayService中自定义的方法。
上面代码中没有定义更多WeixinPayService的代码,需要自己根据需要自行添加。
以上这种方式,适合使用的服务比较少的应用,思路是利用Category从原有的类中抽取了代码另存,这样从AppDelegate中可以容易的跟踪代码。
如果添加的服务很多,那么尽管使用了command+shift+s,还是需要添加和修改比较多的代码。
多个服务之间,代码完全独立,没有任何的复用。多个服务使用上述方法,会使代码量增加的较多。
可以将上述代码进行重构,把多个服务的方法进行统一命名,然后做成服务进行自动调用。
组件中的服务就是应用需要的服务,每个服务只需要按照统一命名的方法进行开发并注册到组件中,那么服务对AppDelegate的回调自动完成。
下一次发基于服务的AppDelegate回调组件。&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
为AppDelegate创建全局访问接口
摘要:开发中经常会用到AppDelegate,那么干脆给它创建一个全局的访问接口吧.Objective-C//创建一个类方法+(AppDelegate*)shareInstance{return(AppDelegate*)[[UIApplicationsharedApplication]delegate];}Swift//创建一个静态的计算属性extensionAppDelegate{staticvarsharedInstace:AppDelegate{returnUIApplic
开发中经常会用到AppDelegate, 那么干脆给它创建一个全局的访问接口吧.
Objective - C
// 创建一个类方法
+ (AppDelegate*)shareInstance {
return (AppDelegate*)[[UIApplication sharedApplication] delegate];
// 创建一个静态的计算属性
extension AppDelegate {
static var sharedInstace: AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
很简单, 不是么.
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
新用户大礼包!
现在注册,免费体验40+云产品,及域名优惠!
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
为AppDelegate创建全局访问接口相关信息,包括
的信息,所有为AppDelegate创建全局访问接口相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
InternationalPages: 1/3
主题 : 如何在 appdelegate.m 中跳转到指定的viewController?
级别: 新手上路
可可豆: 83 CB
威望: 65 点
在线时间: 167(时)
发自: Web Page
来源于&&分类
如何在 appdelegate.m 中跳转到指定的viewController?&&&
如何在 appdelegate.m 中跳转到指定的viewController?    求助  如果用通知中心  该如何编写?  感谢回答
级别: 侠客
UID: 393483
可可豆: 476 CB
威望: 404 点
在线时间: 1403(时)
发自: Web Page
self.window.root..... = vc
级别: 新手上路
可可豆: 83 CB
威望: 65 点
在线时间: 167(时)
发自: Web Page
回 1楼(mnnnnni) 的帖子
&&&&[self.window.rootViewController presentViewController:registerView animated:YES completion:^{&&&&&&&&&&&& }];&&&&&&这样不行的&&&&会报错 whose view is not in the window hierarchy!
级别: 精灵王
发帖: 1300
可可豆: 3259 CB
威望: 3303 点
在线时间: 1492(时)
发自: Web Page
结构是啥样的????
级别: 新手上路
可可豆: 83 CB
威望: 65 点
在线时间: 167(时)
发自: Web Page
回 3楼(1204) 的帖子
我在appdelegate 中创建了一个view&&用于登陆&&&&然后上面有一个注册按钮 点击跳转到注册&&&& 顺便问下 这样合理吗?&& app要求一运行 就要出现登陆页面
级别: 精灵王
发帖: 1300
可可豆: 3259 CB
威望: 3303 点
在线时间: 1492(时)
发自: Web Page
这样做很合理啊,如果跳转的话,去rootviewcontroller里面跳比较好
级别: 新手上路
可可豆: 83 CB
威望: 65 点
在线时间: 167(时)
发自: Web Page
回 5楼(1204) 的帖子
但是我创建的登陆的view&&在app delegate.m中啊&&&&&& 我这样写的 没效果#pragma mark --- 注册按钮响应事件- (void)registerBtnClick{&&&&RegisterViewController *registerView = [[RegisterViewController alloc]init];&&&&[self.window.rootViewController presentViewController:registerView animated:YES completion:^{&&&&&&&&&&&& }];}
级别: 侠客
UID: 393483
可可豆: 476 CB
威望: 404 点
在线时间: 1403(时)
发自: Web Page
回 2楼(我是小文) 的帖子
第一,appdelegate.m 必须有一个rootViewcontroller&& 他是 vc&& 你在这个vc里面presen有啥问题。
级别: 侠客
UID: 393483
可可豆: 476 CB
威望: 404 点
在线时间: 1403(时)
发自: Web Page
回 6楼(我是小文) 的帖子
你不可以什么事情都在appdelegate。m中处理吧&&&&appdelegate里加个按钮 是加在window上的 ,所有页面都有,你确定要这效果?(prsen我没测试有没有,但push一定有)。
级别: 新手上路
可可豆: 83 CB
威望: 65 点
在线时间: 167(时)
发自: Web Page
回 7楼(mnnnnni) 的帖子
&& self.baseTabBarController = [[BaseTabBarController alloc]init];&&&&self.window.rootViewController = _baseTabBarCBaseTabBarController 里有四个viewController 啊&&&& 那该如何 编写呢?
Pages: 1/3
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版}

我要回帖

更多关于 ios appdelegate 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信