如何通过AppDelegate在首次运行时覆盖初始ViewController

时间:2016-02-18 09:18:14

标签: ios swift

我使用以下代码来检测应用程序是否第一次运行:

func Show(){
    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier(“myViewController”) as! MyViewController
    self.window?.rootViewController?.presentViewController(vc, animated: true, completion: nil)
}

我使用以下函数来显示ViewController:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

我将两个代码放在:TypeInfo

但是,当我构建并运行应用程序时,初始视图控制器显示为好像什么也没发生。我想首先显示“myViewController”(在应用程序首次启动设备期间)。然而,在第一次启动之后,“myViewController”将不会再显示,除非用户卸载并重新安装应用程序。 / p>

有谁知道怎么做?

2 个答案:

答案 0 :(得分:1)

由于您希望在首次启动时显示自定义屏幕然后将其关闭,我会在您的普通根VC %{ #include <cstdio> #include <cstring> #include <iostream> #include <stdio.h> #define YYDEBUG 1 using namespace std; extern "C" int yylex(); extern "C" FILE *yyin; extern int line_num; void yyerror(const char* s); %} // Symbols. %union { char* sval; }; %token <sval> TOK_NAME %token <sval> TOK_SIZE %token <sval> TOK_STRING %token <sval> TOK_ITERATE %token <sval> TOK_DIRECTION %token <sval> TOK_STRAP %token <sval> TOK_WRAP %token <sval> TOK_VIA %token <sval> TOK_EMPTY_LINE %token <sval> TOK_BLOCK %token <sval> TOK_LINE %token <sval> TOK_BEGIN %token <sval> TOK_END %token <sval> TOK_VERSION %token <sval> TOK_STRUCT %token <sval> TOK_UNIQUE %token <sval> TOK_REF %token <sval> TOK_POS %token <sval> TOK_CON %token <sval> TOK_ORI %token END ENDL %% language : program ; program : block | program block ; block : TOK_BEGIN TOK_BLOCK TOK_STRING blockcontents TOK_END TOK_STRING { if (strcmp($3,$6) == 0 ) { printf("\nHEADER %s ",$2); printf("\nID %s ",$3); } else { printf("Block %s is not able to find its END\n" , $3); } } | TOK_BEGIN TOK_BLOCK TOK_STRING blockcontents TOK_END { printf("Block %s is not able to find its END\n" , $3); } | TOK_BEGIN TOK_STRING blockcontents TOK_END TOK_STRING {} | TOK_BEGIN TOK_STRUCT TOK_STRING blockcontents TOK_END TOK_STRING { if (strcmp($3,$6) == 0 ) { printf("\nHEADER %s ",$2); printf("\nID %s \n",$3); } else { printf("Block %s is not able to find its END\n" , $3); } } | TOK_BEGIN TOK_STRAP TOK_STRING blockcontents TOK_END TOK_STRING { if (strcmp($3,$6) == 0 ) { printf("\nHEADER %s ",$2); printf("\nID %s \n",$3); } else { printf("Block %s is not able to find its END\n" , $3); } } | TOK_BEGIN TOK_WRAP TOK_STRING blockcontents TOK_END TOK_STRING ; blockcontents : item | blockcontents item ; item : TOK_NAME TOK_STRING { cout << endl << $1 << "->" << $2 << " "; } | TOK_SIZE TOK_STRING { cout << $1 << "->" << $2 << " "; } | TOK_ITERATE TOK_STRING { cout << $1 << "->" << $2 << " "; } | TOK_DIRECTION TOK_STRING { cout << endl << $1 << "->" << $2 << " " << endl; } | TOK_STRAP TOK_STRING { cout << $1 << "->" << $2 << " "; } | TOK_WRAP TOK_STRING { cout << $1 << "->" << $2 << " "; } | TOK_VIA TOK_STRING { cout << $1 << "->" << $2 << " " << endl; } | TOK_VERSION TOK_STRING {} | TOK_UNIQUE TOK_STRING { cout << endl << $1 << "->" << $2 << " " << endl; } | TOK_REF TOK_STRING { cout << endl << $1 << "->" << $2 << " " << endl; } | TOK_POS TOK_STRING { cout << endl << $1 << "->" << $2 << " " << endl; } | TOK_CON TOK_STRING { cout << endl << $1 << "->" << $2 << " " << endl; } | TOK_ORI TOK_STRING { cout << endl << $1 << "->" << $2 << " " << endl; } | block ; %% int main(void) { FILE * pt = fopen("LG.txt", "r" ); if(!pt) { cout << "Bad Input.Noexistant file" << endl; return -1; } yyin = pt; do { //yydebug = 1; yyparse(); }while (!feof(yyin)); } void yyerror(const char *s) { cout << "parse error on line " << line_num << "! Message: " << s << endl; exit(-1); } extern "C" int yywrap() { return (1 == 1); } #include "lex.yy.c" 方法中运行此代码

修改

是的,您必须稍微修改一下代码。例如,像这样:

viewWillAppear(animated:Bool)

答案 1 :(得分:0)

将值设置为用户默认值后,您必须使用synchronize方法进行保存。定期调用此方法购买系统,但如果在调用之前未进行同步,则不会保存您的值。

NSUserDefaults.standardUserDefaults().setBool(true, forKey: "FirstLaunch")
NSUserDefaults.standardUserDefaults().synchronize()

synchronize从Apple文档的文档:

  

将对持久域的任何修改写入磁盘,并将所有未修改的持久域更新为磁盘上的内容。

     

讨论

     

因为此方法是定期自动调用的,所以只有在您不能等待自动同步时(例如,如果您的应用程序即将退出)或者您希望将用户默认值更新为打开时,才使用此方法即使您没有进行任何更改,也可以使用磁盘。