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

标签 ios swift uiviewcontroller uibutton uitabbarcontroller

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

此按钮链接到两 (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 ,方式与两个 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/

相关文章:

ios - (Xamarin iOS) : ExecutionEngineException - Attempting to JIT compile method while running in aot-only mode

ios - 快速在 map 上添加 MKTileOverlay

iOS Alamofire 超时且无网络连接

ios - Swift 2.0 'Element.Protocol' 没有名为 'Hour' 的成员,NSCalendar.components

ios - 如何在许多子类中使用 UITableViewDelegate 对 UIViewController 进行子类化

ios - 从自定义类管理 NSURLSession 的完成处理程序

swift - 无法从 Swift 中的 Realm 对象服务器获取数据

ios - 以编程方式旋转 UIViewController?

ios - 如何从 SKScene 到 UIViewController?

ios - 用于快捷方式崩溃的 3D Touch