Link UIButton 到多个不同选项卡栏控制器中的特定选项卡
Link UIButton To A Specific Tab Within Several Different Tab Bar Controllers
我的应用 index/main 页面上有一个 'Enter' 按钮。
此按钮链接到两 (2) 个不同的标签栏控制器之一 - 取决于用户是否购买了完全访问权限。每个标签栏控制器有 3 个标签。
这两 (2) 个标签栏控制器分别命名为 "TabBarControllerFree" 和 "TabBarControllerPaid"。
我写了下面的代码来确定用户在哪里 'sent':
@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)
}
}
目前,默认情况下将用户发送到相应选项卡栏控制器中的第一个选项卡(索引 0 或最左侧的选项卡)。
我希望可以选择将用户转到第二个选项卡(索引 1)或第三个选项卡(索引 2)。
我意识到我必须将 "SelectedIndex = 1" 或 "SelectedIndex = 2" 影响到我的代码中。
请问我该怎么做?
** 附加信息 **
这是我的故事板地图的快照:
基本上,我的初始 VC(登录页面)上有三 (3) 个按钮(绿色)。
(1) 如果用户点击 'Enter',我会检查他们是否具有完全访问权限,并将它们发送到相应的付费或免费标签栏控制器(红色箭头)。
(2) 如果用户单击 'Workouts',我将它们发送到相同的选项卡栏控制器,方式与 first/left 大多数(索引 0)选项卡都是 'Workouts'(黄色箭头)。
(3) 如果用户单击 'Exercises',我想将它们发送到各自付费或免费选项卡栏控制器(蓝色箭头)中最左边的第二个选项卡(索引 1)。
我正在努力有效执行 'Exercises' 的按钮三 (3)。
为了使其正常工作,您必须更改 Xcode 中的一些设置。 Select 目标。转到“常规”选项卡。在显示主界面的地方,将其留空。
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()
}
// .......
}
我的应用 index/main 页面上有一个 'Enter' 按钮。
此按钮链接到两 (2) 个不同的标签栏控制器之一 - 取决于用户是否购买了完全访问权限。每个标签栏控制器有 3 个标签。
这两 (2) 个标签栏控制器分别命名为 "TabBarControllerFree" 和 "TabBarControllerPaid"。
我写了下面的代码来确定用户在哪里 'sent':
@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)
}
}
目前,默认情况下将用户发送到相应选项卡栏控制器中的第一个选项卡(索引 0 或最左侧的选项卡)。
我希望可以选择将用户转到第二个选项卡(索引 1)或第三个选项卡(索引 2)。
我意识到我必须将 "SelectedIndex = 1" 或 "SelectedIndex = 2" 影响到我的代码中。
请问我该怎么做?
** 附加信息 **
这是我的故事板地图的快照:
基本上,我的初始 VC(登录页面)上有三 (3) 个按钮(绿色)。
(1) 如果用户点击 'Enter',我会检查他们是否具有完全访问权限,并将它们发送到相应的付费或免费标签栏控制器(红色箭头)。
(2) 如果用户单击 'Workouts',我将它们发送到相同的选项卡栏控制器,方式与 first/left 大多数(索引 0)选项卡都是 'Workouts'(黄色箭头)。
(3) 如果用户单击 'Exercises',我想将它们发送到各自付费或免费选项卡栏控制器(蓝色箭头)中最左边的第二个选项卡(索引 1)。
我正在努力有效执行 'Exercises' 的按钮三 (3)。
为了使其正常工作,您必须更改 Xcode 中的一些设置。 Select 目标。转到“常规”选项卡。在显示主界面的地方,将其留空。
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()
}
// .......
}