[英]How to show alert view when tab bar item is pressed and how to ignore it when used in a other target?
我有一個帶有5個標簽的UITabBar
項目。
我正在制作2個目標版本:免費和付費版本。
在免費版本中,當用戶嘗試導航至選項卡項目索引3或4時,將顯示UIAlertView
顯示以下基本消息:
您要升級嗎?
是/取消
當按“ Cancel
按鈕時,視圖應轉到第一個視圖控制器。
我應該怎么做?
另外,我的下一個問題(( 我知道我應該在Stack中再問一個問題 )是如何防止UIAlertView
在付費版本中顯示?
我已經為選項卡項3&4使用UIAlertView
的按鈕了,但是我不想要那樣。
2個目標運行良好,我使用以下代碼:
- (IBAction)openAlert:(id)sender
{
#ifdef FREE
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Attention"
message:@"Choose option"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Download Full version", nil];
[alertView show];
#endif
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex ==1) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://***.com"]]];
}
}
任何幫助,將不勝感激。
將您的appdelegate設置為Tabbar控制器的委托,並在appdelegate或任何地方進行此工作
[yourTabbarController setSelectedIndex:0]
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)項目
取消時,要移動到另一個UIViewController
,只需更改self.tabBarController
對象的setSelectedIndex
例:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
//Cancel button was clicked
[self.tabBarController setSelectedIndex:0];
break;
case 1:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://***.com"]]];
break;
}
}
至於免費還是付費,這是基於意見的。
一種基本的方法是使用NSUserDefaults
記住該應用程序是免費版還是付費版,並相應地處理您的邏輯。
對於第一個問題,您需要利用(void)alertView: (UIAlertView )alertView clickedButtonAtIndex:(NSInteger)buttonIndex
內的警報視圖中單擊的按鈕的索引值,並檢查所按按鈕的索引是否相同作為取消按鈕的索引。 然后,您可以以編程方式按所需視圖的選項卡,然后轉到該視圖。
在加載警報彈出窗口之前,請檢查應用程序的狀態(付費或免費)。 您可以通過兩種方式進行檢查,一種是通過將應用購買狀態存儲在設備上的NSUserDefaults
,另一種是通過服務器身份驗證,盡管服務器身份驗證可能會阻礙應用的使用需要一段時間才能從服務器獲得響應,並且在沒有網絡連接的情況下,用戶將無法使用該應用程序的付費功能。
如果應用程序具有所需的付費狀態,只需讓“付費”標簽中的視圖加載即可,否則僅顯示黑屏。
您可以在已付費標簽的視圖控制器中實施此檢查。 在viewWillAppear
,實施此檢查以獲取付款狀態,然后如果該應用不是付費應用,則顯示黑色視圖並顯示警報消息。 否則,如果該應用程序是付費的,則業務照常進行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.