将字符串传递给另一个视图控制器-Swift3时获取nil响应

将字符串传递给另一个视图控制器-Swift3时获取nil响应,swift3,Swift3,在FirstViewController中,我从JSON获取响应,并希望将获取的响应传递给另一个视图控制器 FirstViewController var fn:String! //globally declared variable 我尝试在FirstViewController中解析的代码 do { let detailsDictionary:NSDictionary = try JSONSerialization.jsonObject(with: data!, option

在FirstViewController中,我从JSON获取响应,并希望将获取的响应传递给另一个视图控制器

FirstViewController

var fn:String!  //globally declared variable
我尝试在FirstViewController中解析的代码

 do {

    let detailsDictionary:NSDictionary = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! Dictionary<String, AnyObject> as NSDictionary


    print(detailsDictionary)
    let details = detailsDictionary["Data"] as! [[String:AnyObject]]
    print(details)
    for dtl in details
    {
        self.fn = dtl["Father_Name"] as? String ?? "NA"
        print(self.fn)  //here i'm getting the exact value from JSON        
     }

    }
}
下面是我用来从FirstViewController获取值的代码

viewDidLoad()
{
       let othervc = FirstViewController()
       self.pname = othervc.fn
       self.profile_name.text = self.pname
}
问题:我尽了最大努力获得了所需的输出,但没有得到任何回应。


请帮助。

在第二视图控制器中

let strName:String!
let strOne = "This is for testing"

let objstory = self.storyboard?.instantiateViewController(withIdentifier: "yout Secoond ViewController Storybord ID") as! YourSecondViewControllerName
objstory.strNam = strOne     

self.navigationController?.pushViewController(objstory, animated: true)
在第一个ViewController中

let strName:String!
let strOne = "This is for testing"

let objstory = self.storyboard?.instantiateViewController(withIdentifier: "yout Secoond ViewController Storybord ID") as! YourSecondViewControllerName
objstory.strNam = strOne     

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

你更新的代码就是不起作用

let othervc = FirstViewController() 
创建
FirstViewController
新实例(不是获取JSON的实例)

您应该像这样处理它:

FirstViewController

let fn = dtl["Father_Name"] as? String ?? "NA"
let svc = SecondViewController() // Or maybe instantiate from Storyboard, or maybe you already have a reference to it
svc.pname = fn
present(svc, animated: true, completion: nil)
override func viewDidLoad() {
    super.viewDidLoad()

    profile_name.text = pname
}
override func viewDidLoad() {
    super.viewDidLoad()

    profile_name.text = name
}
然后在
SecondViewController

let fn = dtl["Father_Name"] as? String ?? "NA"
let svc = SecondViewController() // Or maybe instantiate from Storyboard, or maybe you already have a reference to it
svc.pname = fn
present(svc, animated: true, completion: nil)
override func viewDidLoad() {
    super.viewDidLoad()

    profile_name.text = pname
}
override func viewDidLoad() {
    super.viewDidLoad()

    profile_name.text = name
}
我建议你花点时间重新阅读


原始答案

你这里的问题

vcvalue.profile_name.text = fn
profile\u name
nil
,因为此时尚未加载视图控制器的视图

您应该通过在
LeftSideMenuViewController

var name: String?
然后设置

vcvalue.name = fn
然后在
LeftSideMenuViewController

let fn = dtl["Father_Name"] as? String ?? "NA"
let svc = SecondViewController() // Or maybe instantiate from Storyboard, or maybe you already have a reference to it
svc.pname = fn
present(svc, animated: true, completion: nil)
override func viewDidLoad() {
    super.viewDidLoad()

    profile_name.text = pname
}
override func viewDidLoad() {
    super.viewDidLoad()

    profile_name.text = name
}
还有,一些基本提示

  • 不要强行将展开(!)与出口分开。您可能需要编写更多的代码,但这样可以减少崩溃
  • 使
    @IBOutlet
    s
    private
    -这将防止您意外地按现在的方式分配给他们
  • 如果要覆盖任何
    viewWill/DidDis/appeand
    方法,则必须在某个时候调用
    super
  • 您需要重新阅读
    开关/机箱
所以这个

let  a = indexPath.row
switch(a)
    {
    case 0 :
        if(a == 0)
        {
            return 45
        }
        break
     etc
可能只是

switch indexPath.row {
case 0...4:
    return 45
case 5:
    return 50
default: 
    break
}

我尝试了你的代码,但仍然无法设置标签的文本。感谢您的快速响应。请尝试在viewDidLoad中设置yourlableName.text=strName第二个ViewController的加载方法已在viewDidLoad中设置:(.未获得任何错误,但标签仍然为空:(.我已使用secondviewcontroller更新了我的问题。不是标签的值m将fn传递给另一个视图控制器的标签文本。请从此行operationqueue.main.addoperation检查firstviewcontroller中的代码。您的“profile_name”标签是否作为出口连接?因为如果出现此错误:“在展开可选值时意外发现零”,可能您的插座未连接。插座已连接。请仅在问题中显示相关代码。例如,为什么您认为
heightForRowAt
的内容将帮助人们解决您的问题?问题的原因很可能是
FirstViewController()
。默认初始值设定项创建的新实例不是故事板中设计的实例。在这个新实例中,
fn
nil
。您必须在视图层次结构中获得适当的实例,包括outlet、callback closure、protocol/delegate或notification不需要其他东西吗?不,只需执行与您相同的操作,但不要从第一视图控制器分配插座(事实上,将所有IBOutlet设置为私有以防止此情况发生)先生,我仍然无法获得所需的输出:(.先生,我遵循了相同的步骤,但在LeftSideMenuViewController中,我得到了零值。先生,请告诉我,我不知道我丢失了什么。还有一件事我想和大家分享的是,我在dispatchqueue.main.async块的viewWillAspect方法中添加了这个profile_name.text=pname。我更新了我的问题。谢谢先生,关于如何询问问题的指导问题。如果你能帮我解决这个问题就太好了。