【问题标题】:Link UIButton To A Specific Tab Within Several Different Tab Bar Controllers将 UIButton 链接到几个不同选项卡栏控制器中的特定选项卡
【发布时间】:2016-07-31 04:25:59
【问题描述】:

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

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

这两 (2) 个标签栏控制器分别命名为“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)

    }

}

目前,默认情况下,用户会被发送到各个选项卡栏控制器中的第一个选项卡(索引 0 或最左侧的选项卡)。

我想选择将用户发送到第二个选项卡(索引 1)或第三个选项卡(索引 2)。

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

请问我该怎么做?

** 附加信息 **

这是我的故事板地图的快照:

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

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

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

(3) 如果用户点击“练习”,我想将它们发送到相应的付费或免费标签栏控制器(蓝色箭头)中最左边的第二个标签(索引 1)。

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

【问题讨论】:

    标签: ios swift uiviewcontroller uibutton uitabbarcontroller


    【解决方案1】:

    为了使它工作,您必须在 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()
    
    
        }
        // .......
    }
    

    【讨论】:

    • 不能。出现此错误:Cannot convert value of type '()' to expected argument type 'UIViewController'。在我的代码中,我认为我必须将一个变量声明为“UITabBarController”才能采用该属性?
    • 我的错。请注意编辑的答案。我已经在我的一个应用程序中使用了它。
    • 我已经尝试过代码,它只是将我发送到索引 0 处的 VC(我认为默认情况下?)。这是我的代码:let vc = self.storyboard!.instantiateViewControllerWithIdentifier("TabBarControllerFree") as! TabBarControllerFree ... 下一行 ... self.presentViewController(vc.tabBarController?.selectedIndex = 2, animated: true, completion: nil)
    • 我已尝试使用以下代码直接链接到我想要作为目标的索引处的特定 VC:let vc = self.storyboard!.instantiateViewControllerWithIdentifier("WorkoutsAllFreeVC") as! WorkoutsAllFreeVC ... 下一行 ... self.presentViewController(vc, animated: true, completion: nil)。这会将用户发送到所需的目标 VC,但我丢失了底部的标签栏。
    • 这个 TabBar 应该是根控制器吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2014-12-08
    • 2016-02-25
    相关资源
    最近更新 更多