Ios 将UIButton链接到几个不同选项卡栏控制器中的特定选项卡
我的应用程序索引/主页上有一个“回车”按钮 此按钮链接到两(2)个不同选项卡栏控制器中的一个,具体取决于用户是否购买了完全访问权限。每个选项卡栏控制器有3个选项卡 这两(2)个选项卡栏控制器分别命名为“TabBarControllerFree”和“TabBarControllerPaid” 我编写了以下代码来确定用户被“发送”到哪里:Ios 将UIButton链接到几个不同选项卡栏控制器中的特定选项卡,ios,swift,uiviewcontroller,uibutton,uitabbarcontroller,Ios,Swift,Uiviewcontroller,Uibutton,Uitabbarcontroller,我的应用程序索引/主页上有一个“回车”按钮 此按钮链接到两(2)个不同选项卡栏控制器中的一个,具体取决于用户是否购买了完全访问权限。每个选项卡栏控制器有3个选项卡 这两(2)个选项卡栏控制器分别命名为“TabBarControllerFree”和“TabBarControllerPaid” 我编写了以下代码来确定用户被“发送”到哪里: @IBAction func Enter(sender: AnyObject) { //Check if product is purchased
@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) 如果用户单击“输入”,我将检查他们是否具有完全访问权限,并将其发送到相应的付费或免费选项卡栏控制器(红色箭头)
(2) 如果用户单击“训练”,我将它们发送到相同的选项卡栏控制器,发送方式与两个选项卡上的第一个/最左侧(索引0)选项卡都是“训练”(黄色箭头)相同
(3) 如果用户单击“练习”,我想将它们发送到相应付费或免费选项卡栏控制器(蓝色箭头)中最左边的第二个选项卡(索引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()
}
// .......
}
不能。出现此错误:
无法将“()”类型的值转换为预期的参数类型“UIViewController”
。在我的代码中,我想我必须声明一个变量为'UITabBarController'才能采用这个属性?我的错。请注意编辑后的答案。我在我的一个应用程序中使用过这个。我尝试过这个代码,它只会将我发送到索引为0的VC(我想是默认的?)。这是我的代码:let vc=self.storyboard!。实例化eviewcontrollerwhiteIdentifier(“TabBarControllerFree”)为!TabBarControllerFree
。。。下一行<代码>self.presentViewController(vc.tabBarController?.selectedIndex=2,动画:true,完成:nil)我尝试直接链接到我想要作为目标的索引处的特定vc,代码为:让vc=self.storyboard!。实例化EviewController的标识符(“WorkoutsAllFreeVC”)为!WorkoutsAllFreeVC
。。。下一行<代码>self.presentViewController(vc,动画:true,完成:nil)。这会将用户发送到所需的目标VC,但我丢失了底部的选项卡栏。这个选项卡栏应该是根控制器吗?