我开始看一下新Swift的Xcode 6,我尝试了一些演示项目和教程.现在我被困在:
实例化然后viewController从特定的故事板中呈现
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"myVCID"];
[self presentViewController:vc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
如何在Swift上实现这一目标?
我正在从在线课程学习iOS开发,每当我创建自定义视图(自定义表格视图单元格,集合视图单元格等)时,教师总是实现此初始化程序:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
Run Code Online (Sandbox Code Playgroud)
为什么我总是要打电话给这个?它有什么作用?我可以在init中放置属性吗?
我正在尝试使用swift以编程方式创建一个简单的tableView,所以我在"AppDelegate.swift"上编写了这段代码:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var tvc :TableViewController = TableViewController(style: UITableViewStyle.Plain)
self.window!.rootViewController = tvc
self.window!.backgroundColor = UIColor.whiteColor()
self.window!.makeKeyAndVisible()
return true
}
Run Code Online (Sandbox Code Playgroud)
基本上我添加了TableViewController创建并添加到窗口.这是TableViewController代码:
class TableViewController: UITableViewController {
init(style: UITableViewStyle) {
super.init(style: style)
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// #pragma mark - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView?) -> Int {
return 1
} …Run Code Online (Sandbox Code Playgroud) 今天的扩展程序没有出现在Swift应用程序中,但它在Objective C应用程序中出现.
我所做的是在故事板上为快速和客观的c应用添加一些带有一些内容的UILabel.
它出现在我运行Objective C应用程序时,但不是在我执行Swift应用程序时.
我在这里错过了什么吗?
我一直试图将UIView子类化并使用/sf/answers/1682550831/的帮助来处理'init(编码器)'问题
然而,即使是最基本的尝试,我仍然遇到障碍,我希望有人可以发布一个简单的例子,说明用Swift子类化UIView所需的实际代码.
我目前的代码是
//image class
import UIKit
class childView: UIView {
var image:UIImage!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
Run Code Online (Sandbox Code Playgroud)
我不想传递这个NSCoder对象(我不知道它做了什么,它让我的代码变得混乱).你能发布一个如何干净地子类化UIView的例子,然后你将如何在另一个类中初始化它?
我真的很想扩展我的UIView类,以获得一些很酷的效果,如阴影,自动缩放和许多其他商品,而无需创建子视图来处理所有这些(如UIScrollViews中的UIImageViews)
我有一个tableView使用继承的单元格。有一个共同的祖先,然后有几种具体的实现。公共类不会重写init(style:reuseIdentifier:),而是定义自己的自定义初始值设定项。然而,所有子类都定义了这个初始值设定项,它们在其中从超类中调用自定义的初始值设定项。现在,tableView我注册子类以供重用,并且将子类出队以供重用。然而,当我尝试将任何子类出队时,出现以下错误:
fatal error: use of unimplemented initializer 'init(style:reuseIdentifier:)' for class 'milan.BotChatBaseCell'
Run Code Online (Sandbox Code Playgroud)
milan.BotChatBaseCell超类在哪里。现在milan.BotChatBaseCell永远不会注册到tableView。知道为什么会发生这种情况吗?
代码:
import UIKit
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(ExampleBotChatCell.self, forCellReuseIdentifier: "ExampleBotChatCell")
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleBotChatCell", for: indexPath) as! ExampleBotChatCell
return cell
}
}
class BotChatBaseCell: UITableViewCell {
init(style: …Run Code Online (Sandbox Code Playgroud) 我已经将自定义UIButton编码为:
class AccountOpeningButton: UIButton {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
......
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用我的故事板成功实例化该类。现在,我做了一个UIView并想在我的UIView中添加此按钮为:
var customView:UIView = UIView()
customView.frame = CGRect(x: 0, y: 0, width: 350, height: 250)
.....
let fromDateBtn:UIButton = AccountOpeningButton()//Error comes here as : Missing Argument for parameter ‘coder’ in call
customView.addSubview(fromDateBtn)
Run Code Online (Sandbox Code Playgroud)
因此,也请帮助动态地重用此代码。
PS:我提到了http://napora.org/nscoder-and-swift-initialization/ 致命错误:对类 Class 使用未实现的初始化器“ init(coder :)” 无法实现其超类的必需成员 ,但未成功。
================================================== ==================== 尝试
let fromDateBtn:UIButton = UIButton() as! AccountOpeningButton
Run Code Online (Sandbox Code Playgroud)
这将引发CastException 无法将类型为'UIButton'的值转换为'.AccountOpeningButton'