生活随笔
收集整理的這篇文章主要介紹了
iPhone Development Blog系列: 如何制作服务条例窗口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
iPhone Development Blog系列: 如何制作服務條例窗口
最近一直關注iPhone Development Blog上面的文章,學習的同時嘗試通過翻譯和整理同大家一起分享!
假設你想讓你的每個客戶在使用iPhone應用前接受你的服務條例(Terms of Services)或其他一些法律聲明。
在啟動時顯示
通過Xcode生成的標準AppDelegate代碼會創建并顯示你的第一個應用界面。這是一個你可以通過UIView的hidden屬性在不用打斷應用接下來的流程情況下顯示新界面的地方。
?self.window?=?[[[UIWindow?alloc]?initWithFrame:[[UIScreen?mainScreen]?bounds]]?autorelease]; ?[window?setBackgroundColor:[UIColor?whiteColor]]; ???MainMenuViewController?*navController?=?[[MainMenuViewController?alloc]?init]; ???navigationController?=?[[UINavigationController?alloc]?initWithRootViewController:navController]; ?navigationController.navigationBarStyle?=?UIBarStyleDefault; ??[navController?release]; ???tosController?=?[[TermsOfServiceViewController?alloc]?init]; ?[window?addSubview:[tosController?view]]; ?navigationController.view.hidden?=?YES; ???[window?addSubview:[navigationController?view]]; ?[window?makeKeyAndVisible]; ? 大部分的基礎代碼都是Xcode生成的。新增代碼從第14行開始。實例化TermsOfServiceViewController 并加入到視圖中。重要的是第17行,它隱藏了navigationController。
關閉新窗口
現在已經顯示了服務條例窗口并且主菜單已經隱藏。我們需要關閉服務條例窗口并回到正常的應用流程。
在AppDelegate類中創建一個新方法:
-?(void)termsOfServiceAccepted ?{ ??tosController.view.hidden?=?YES; ??navigationController.view.hidden?=?NO; ?} ?
在TermsOfServiceController中需要添加一個用戶點擊的按鈕:
?
UIButton?*acceptButton?=?[[UIButton?buttonWithType:UIButtonTypeRoundedRect]?retain]; ?acceptButton.frame?=?CGRectMake(kLeftMargin, ???????applicationFrame.size.height?-?kBottomMargin?-?kButtonHeight, ???????applicationFrame.size.width?-?kLeftMargin?-?kRightMargin, ???????kButtonHeight); ?[acceptButton?setTitle:NSLocalizedString(@"ButtonAcceptTermsOfService",?@"")?forStates:UIControlStateNormal]; ?[acceptButton?addTarget:self?action:@selector(termsOfServiceAccepted:)?forControlEvents:UIControlEventTouchUpInside]; ?[self.view?addSubview:acceptButton]; ? 當點擊按鈕后,就會調用相應的選擇器并調用上述提到的AppDelegate中的方法。
?
-?(void)termsOfServiceAccepted:(id)sender ?{ ??id?applicationDelegate?=?[[UIApplication?sharedApplication]?delegate]; ??[applicationDelegate?termsOfServiceAccepted]; ?}?
只顯示一次
利用上述代碼服務條例窗口會在每次程序啟動的時候都顯示。這會很煩人。我們需要加些代碼來使其只在NSUserDefaults中的布爾中為YES的時候顯示。.
?
NSUserDefaults?*userDefaults?=?[NSUserDefaults?standardUserDefaults]; ?if?(![userDefaults?boolForKey:TERMS_OF_USE_ACCEPTED])?{ ??tosController?=?[[TermsOfServiceViewController?alloc]?init]; ??[window?addSubview:[tosController?view]]; ??navigationController.view.hidden?=?YES; ?}?
在termsOfServiceAccepted方法中設置存儲在NSUserDefaults內的布爾值。
?
-?(void)termsOfServiceAccepted ?{ ??tosController.view.hidden?=?YES; ??navigationController.view.hidden?=?NO; ?????NSUserDefaults?*userDefaults?=?[NSUserDefaults?standardUserDefaults]; ??[userDefaults?setBool:YES?forKey:TERMS_OF_USE_ACCEPTED]; ?} ?
這樣只要用戶點擊接受按鈕后服務條例窗體就不顯示了。你的客戶和法律顧問都會滿意的。
原文出處:http://iphoneincubator.com/blog/windows-views/how-to-create-a-terms-of-service-screen
轉載于:https://blog.51cto.com/bj007/412477
總結
以上是生活随笔為你收集整理的iPhone Development Blog系列: 如何制作服务条例窗口的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。