使用导航控制器在故事板中呈现视图控制器 - Swift

时间:2014-08-15 12:46:18

标签: ios uiviewcontroller uinavigationcontroller swift uistoryboard

我目前在下面的新故事板中展示了一个viewController:

var storyboard : UIStoryboard = UIStoryboard(name: AccountStoryboard, bundle: nil)
var vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"        
self.presentViewController(vc, animated: true, completion: nil)

但是,这会使viewcontroller没有嵌入式导航控制器。 我尝试将“WelcomeID”更改为故事板中的导航控制器 - 但是没有成功。

我在Objective -C中使用了这个,但是不知道如何转换为swift:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"SetupStoryboard" bundle:nil];
UINavigationController *navigationController1 = [storyboard instantiateInitialViewController];
navigationController1.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController1.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

WelcomeViewController *vc = (WelcomeViewController *)navigationController1.viewControllers[0];
vc.teststring = @"Hello";

[self presentViewController:navigationController1 animated:YES completion:nil];

你怎么能在swift中做到这一点?

5 个答案:

答案 0 :(得分:92)

你肯定是在正确的轨道上。不幸的是,当您通过其故事板ID引用视图控制器时,它将忽略它嵌入任何内容的事实。对于segue,当你转向嵌入的东西时,目标视图控制器将是嵌入式控制器,而不是你通常感兴趣的控制器。无论如何,你应该能够以类似的方式修复问题。 Objective-C,所以这只是语法移植的练习。

编辑:现在使用字符串定义故事板名称

let storyboard : UIStoryboard = UIStoryboard(name: "AccountStoryboard", bundle: nil)
let vc : WelcomeViewController = storyboard.instantiateViewControllerWithIdentifier("WelcomeID") as WelcomeViewController
vc.teststring = "hello"        

let navigationController = UINavigationController(rootViewController: vc)

self.presentViewController(navigationController, animated: true, completion: nil)

或者您可以为嵌入视图控制器提供一个ID并将其实例化。

答案 1 :(得分:17)

 let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("WelcomeID") as SecondViewController

        self.navigationController?.pushViewController(secondViewController, animated: true)

类名是:SecondCiewController

Identifier Name

答案 2 :(得分:9)

@Chris给出的答案在较旧版本的swift中效果很好。

更新Swift 3&斯威夫特4

from pyspark.sql.functions import UserDefinedFunction
from pyspark.sql.functions import *

udf = UserDefinedFunction(lambda x: float(x.replace(",",".")), DoubleType())

out = df
    .withColumn("Atr1", udf(col("Atr1")))
    .withColumn("Atr2", udf(col("Atr2")))

感谢!!!

答案 3 :(得分:0)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "SalesVC") as! SalesVC

navigationController?.pushViewController(vc, animated: true)

答案 4 :(得分:0)

请记住要设置modalPresentationStyle属性,以编程方式表示将忽略IB设置。

{{1}}