gpt4 book ai didi

ios - 将 UIButton 链接到多个不同选项卡栏 Controller 中的特定选项卡

转载 作者:可可西里 更新时间:2023-11-01 01:36:43 30 4
gpt4 key购买 nike

我的应用索引/主页上有一个“进入”按钮。

此按钮链接到两 (2) 个不同的标签栏 Controller 之一 - 取决于用户是否购买了完全访问权限。每个标签栏 Controller 有 3 个标签。

这两 (2) 个标签栏 Controller 分别命名为“TabBarControllerFree”和“TabBarControllerPaid”。

我编写了以下代码来确定用户被“发送”到哪里:

@IBAction func Enter(sender: AnyObject) {

//Check if product is purchased
if (defaults.boolForKey("purchased")){
print("User has purchased")

// Send User To Paid TabBarController - FULL Access:

let vc = self.storyboard!.instantiateViewControllerWithIdentifier("TabBarControllerPaid") as! TabBarControllerPaid
self.presentViewController(vc, animated: true, completion: nil)


}

else if (!defaults.boolForKey("purchased")){
print("user has NOT purchased")

// Send User To Free TabBarController - PARTIAL Access:
let vc = self.storyboard!.instantiateViewControllerWithIdentifier("TabBarControllerFree") as! TabBarControllerFree
self.presentViewController(vc, animated: true, completion: nil)

}

}

目前,默认情况下将用户发送到相应选项卡栏 Controller 中的第一个选项卡(索引 0 或最左侧的选项卡)。

我希望可以选择将用户转到第二个选项卡(索引 1)或第三个选项卡(索引 2)。

我意识到我必须在我的代码中实现“SelectedIndex = 1”或“SelectedIndex = 2”。

请问我该怎么做?

** 附加信息 **

这是我的 Storyboard map 的快照:

enter image description here

基本上,我的初始 VC(登录页面)上有三 (3) 个按钮(绿色)。

(1) 如果用户单击“Enter”,我会检查他们是否具有完全访问权限,并将它们发送到相应的付费或免费标签栏 Controller (红色箭头)。

(2) 如果用户单击“锻炼”,我会将它们发送到相同的选项卡栏 Controller ,因为两者的第一个/最左侧(索引 0)选项卡都是“锻炼”(黄色箭头)。

(3) 如果用户单击“练习”,我想将它们发送到各自付费或免费选项卡栏 Controller (蓝色箭头)中最左边的第二个选项卡(索引 1)。

我努力有效地执行“练习”的按钮三 (3)。

最佳答案

为了使其正常工作,您必须更改 Xcode 中的一些设置。选择目标。转到“常规”选项卡。在显示主界面的地方,将其留空。

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
// Add this
var mainViewController: UITabBarController?
var isPaid: Bool {
get {
let defaults = NSUserDefaults.standardUserDefaults()
let saveIt = defaults.objectForKey("isPaid") as? Bool ?? false
return saveIt
}
set(newBool) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(newBool, forKey: "isPaid")
}
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.

self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

if isPaid {
mainViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TabControllerPaid") as? UITabBarController
}
else {
mainViewController = mainStoryboard.instantiateViewControllerWithIdentifier("TabControllerFree") as? UITabBarController
}

mainViewController?.selectedIndex = 0
self.window?.rootViewController = mainViewController
self.window?.makeKeyAndVisible()


}
// .......
}

关于ios - 将 UIButton 链接到多个不同选项卡栏 Controller 中的特定选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36524461/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com