欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS开发之应用首次启动显示用户引导

程序员文章站 2022-06-05 08:42:46
这个功能的重点就是在如何判断应用是第一次启动的. 其实很简单 我们只需要在一个类里面写好用户引导页面  基本上都是使用uiscrollview 来实现, 新建一个继承...

这个功能的重点就是在如何判断应用是第一次启动的. 其实很简单

我们只需要在一个类里面写好用户引导页面  基本上都是使用uiscrollview 来实现,

新建一个继承于uiviewcontroller的类 命名为 userguideviewcontroller ,

在userguideviewcontroller.m 写


 1 - (void)viewdidload
 2 {
 3     [super viewdidload];
 4     // do any additional setup after loading the view.
 5     self.view.backgroundcolor = [uicolor redcolor];
 6    
 7     [self initguide];   //加载新用户指导页面
 8 }
 9
10 - (void)initguide
11 {
12     uiscrollview *scrollview = [[uiscrollview alloc] initwithframe:cgrectmake(0, 0, 320, 640)];
13     [scrollview setcontentsize:cgsizemake(1280, 0)];
14     [scrollview setpagingenabled:yes];  //视图整页显示
15     //    [scrollview setbounces:no]; //避免弹跳效果,避免把根视图露出来
16    
17     uiimageview *imageview = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, 320, 460)];
18     [imageview setimage:[uiimage imagenamed:@"0.png"]];
19     [scrollview addsubview:imageview];
20     [imageview release];
21    
22     uiimageview *imageview1 = [[uiimageview alloc] initwithframe:cgrectmake(320, 0, 320, 460)];
23     [imageview1 setimage:[uiimage imagenamed:@"1.png"]];
24     [scrollview addsubview:imageview1];
25     [imageview1 release];
26    
27     uiimageview *imageview2 = [[uiimageview alloc] initwithframe:cgrectmake(640, 0, 320, 460)];
28     [imageview2 setimage:[uiimage imagenamed:@"2.png"]];
29     [scrollview addsubview:imageview2];
30     [imageview2 release];
31    
32     uiimageview *imageview3 = [[uiimageview alloc] initwithframe:cgrectmake(960, 0, 320, 460)];
33     [imageview3 setimage:[uiimage imagenamed:@"3.png"]];
34     imageview3.userinteractionenabled = yes;    //打开imageview3的用户交互;否则下面的button无法响应
35     [scrollview addsubview:imageview3];
36     [imageview3 release];
37    
38     uibutton *button = [uibutton buttonwithtype:uibuttontypecustom];//在imageview3上加载一个透明的button
39     [button settitle:nil forstate:uicontrolstatenormal];
40     [button setframe:cgrectmake(46, 371, 230, 37)];
41     [button addtarget:self action:@selector(firstpressed) forcontrolevents:uicontroleventtouchupinside];
42     [imageview3 addsubview:button];
43    
44     [self.view addsubview:scrollview];
45     [scrollview release];
46 }
button的方法

1 - (void)firstpressed
2 {
3     [self presentmodalviewcontroller:[[[weiboviewcontroller alloc] init] autorelease] animated:yes];  //点击button跳转到根视图
4 }
 

至于添加button是因为我的用户引导最后一个页面有一个画上去的button,写着 开始使用  我在上面添加一个透明的button 用以实现调用方法

 

打开appdelegate.m

首先引入头文件


1 #import "userguideviewcontroller.h"
2 #import "weiboviewcontroller.h"
weiboviewcontroller是我的根视图application: didfinishlaunchingwithoptions: 方法内进行判断


 1 - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
 2 {
 3     self.window = [[[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];
 4     // override point for customization after application launch.
 5    
 6     //判断是不是第一次启动应用
 7     if(![[nsuserdefaults standarduserdefaults] boolforkey:@"firstlaunch"])
 8     {
 9         [[nsuserdefaults standarduserdefaults] setbool:yes forkey:@"firstlaunch"];
10         nslog(@"第一次启动");       
11         //如果是第一次启动的话,使用userguideviewcontroller (用户引导页面) 作为根视图
12         userguideviewcontroller *userguideviewcontroller = [[userguideviewcontroller alloc] init];
13         self.window.rootviewcontroller = userguideviewcontroller;
14         [userguideviewcontroller release];
15     }
16     else
17     {
18         nslog(@"不是第一次启动");
19         //如果不是第一次启动的话,使用loginviewcontroller作为根视图
20         weiboviewcontroller *weiboviewcontroller = [[weiboviewcontroller alloc] init];
21         self.window.rootviewcontroller = weiboviewcontroller;
22         [weiboviewcontroller release];
23
24     }
25    
26     self.window.backgroundcolor = [uicolor whitecolor];
27     [self.window makekeyandvisible];
28     return yes;
29 }