问题描述
我正在尝试开始编写Swift,并且试图从没有运气的模态视图控制器中获取值。
我有两个控制器, ViewController和modalViewController。
在ViewController中,我有一个UITableView并按下按钮打开了modalViewController。
然后从UITextField我传递值。
我已经实现了带有委托和func的协议,但是在某些地方我遗漏了某些东西或出错了。 b
import UIKit
类ViewController:UIViewController,UITableViewDelegate,modalViewControllerDelegate {
@IBOutlet变量表:UITableView !
var tableData = [第一行,第二行,第三行]
覆盖func viewDidLoad(){
super.viewDidLoad()
//做任何加载视图后进行其他设置,通常是从笔尖进行。
}
覆盖func viewDidAppear(动画:Bool){
table.reloadData()
}
覆盖func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处置所有可以重新创建的资源。
}
func tableView(table:UITableView?,numberOfRowsInSection部分:Int)-> Int
{
return tableData.count
}
func tableView(table:UITableView?,cellForRowAtIndexPath indexPath:NSIndexPath!)-> UITableViewCell!
{
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default,reuseIdentifier: cell)
cell.textLabel?.text = tableData [indexPath.row]
返回单元格
}
func sendText(text:NSString){
tableData.append(text)
}}
modalViewController.swift
导入UIKit
协议modalViewControllerDelegate {
func sendText(var text:NSString)
}
modalViewController:UIViewController {
let代表:modalViewControllerDelegate?
@IBOutlet var textField:UITextField?
@IBAction func cancelButton(发送者:AnyObject){
dismissViewControllerAnimated(true,完成:nil)
}
@IBAction func saveButton(发送者:AnyObject){
代表?.sendText(self.textField!.text)
dismissViewControllerAnimated(true,完成:nil)
}
覆盖func viewDidLoad(){
super.viewDidLoad()
//在此处查看设置。
}
}
我的代码没有错误,委托不起作用,始终为零。
谢谢。
您必须在第一个视图控制器中分配委托。
另外,您还必须将 let委托:modalViewControllerDelegate?
更改为var,否则您将无法更改它。
现在您的代表为空。
目前尚不清楚您如何访问ModalViewController。如果您使用的是segues:
重写func prepareForSegue(segue:UIStoryboardSegue,发送者:AnyObject?){
if segue.identifier == modalViewControllerSegue {
var destination = segue.destinationViewController as CategoryViewController
destination.delegate = self
}
}
或者如果您通过编程方式进行操作:
var modalViewController = ModalViewController(parameters)
modalViewController.delegate = self
presentViewController(modalViewController,animation:true,complete:nil)
故事板标识符:
让目的地= UIStoryboard.mainStoryboard()。instantiateViewControllerWithIdentifier ( ModalViewController)为ModalViewController
代表=自我
showViewController(目的地,发送者:无)
编辑:
如果要访问ModalViewCon通过选择一个单元格,您需要 tableView:didSelectRowAtIndexPath
方法:
func tableView(tableView:UITableView,didSelectRowAtIndexPath indexPath:NSIndexPath){
performSegueWithIdentifier( modalViewControllerSegue,发件人:self)
}
使用此方法,您将需要 prepareForSegue
方法来设置委托。
i'm trying to start writing Swift and i'm trying to get a value from a modal view controller with no luck.
I have two controllers, the ViewController and modalViewController.
In ViewController i have a UITableView and with a press of a button i open the modalViewController.
Then from a UITextField i pass the value.I have implement a protocol with delegate and func but somewhere i'm missing something or had it wrong.
ViewController.swift
import UIKit
class ViewController: UIViewController,UITableViewDelegate,modalViewControllerDelegate{
@IBOutlet var table: UITableView!
var tableData = ["First Row","Second Row","Third Row"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
table.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(table:UITableView?,numberOfRowsInSection section: Int) -> Int
{
return tableData.count
}
func tableView(table:UITableView?,cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default,reuseIdentifier:"cell")
cell.textLabel?.text = tableData[indexPath.row]
return cell
}
func sendText(text: NSString) {
tableData.append(text)
} }
modalViewController.swift
import UIKit
protocol modalViewControllerDelegate {
func sendText(var text: NSString)
}
class modalViewController: UIViewController{
let delegate: modalViewControllerDelegate?
@IBOutlet var textField: UITextField?
@IBAction func cancelButton(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
@IBAction func saveButton(sender: AnyObject) {
delegate?.sendText(self.textField!.text)
dismissViewControllerAnimated(true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
I have no errors in the code, the delegate is not working, it's always nil.
Thanks.
You have to assign the delegate in your first view controller.Also, you have to change let delegate: modalViewControllerDelegate?
to a var, or else you can't change it.
Right now your delegate is empty.
It's unclear how you're accessing ModalViewController. If you're using segues:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "modalViewControllerSegue" {
var destination = segue.destinationViewController as CategoryViewController
destination.delegate = self
}
}
Or if you're doing it programmatically:
var modalViewController = ModalViewController(parameters)
modalViewController.delegate = self
presentViewController(modalViewController, animated: true, completion: nil)
Storyboard identifier:
let destination = UIStoryboard.mainStoryboard().instantiateViewControllerWithIdentifier("ModalViewController") as ModalViewController
delegate = self
showViewController(destination, sender: nil)
EDIT:
If you want to access ModalViewController by selecting a cell you need the tableView: didSelectRowAtIndexPath
method:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("modalViewControllerSegue", sender: self)
}
Using this, you'll need the method prepareForSegue
to set the delegate.
这篇关于在Swift iOS中从Modal View获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!