我的应用索引/主页上有一个“输入”按钮。
此按钮链接到两 (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 的快照:
基本上,我的初始 VC(登陆页面)上有三 (3) 个按钮(绿色)。
(1) 如果用户单击“Enter”,我会检查他们是否具有完全访问权限,并将它们发送到相应的付费或免费选项卡栏 Controller (红色箭头)。
(2) 如果用户单击“锻炼”,我会将它们发送到相同的选项卡栏 Controller ,方式与两个 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/