文件之间的Swift Pass数据

时间:2015-05-11 19:41:00

标签: ios swift

我需要将变量值从文件传递到另一个文件。  我尝试使用此代码,但它无法在目标数据上运行import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Gui extends JFrame{ private JButton add; private JButton subtract; private JButton multiply; private JButton divide; private JTextField box1; private JTextField box2; private JLabel label; private JButton ansbutton; public String operator; public Gui(){ super("Luke's Calculator"); setLayout(new FlowLayout()); add = new JButton("Add"); subtract = new JButton("Subtract"); multiply = new JButton("Multiply"); divide = new JButton("Divide"); add(add); add(subtract); add(multiply); add(divide); box1 = new JTextField(10); label = new JLabel(); box2 = new JTextField(10); ansbutton = new JButton("Answer"); add(box1); add(label); add(box2); add(ansbutton); HandlerClass handler = new HandlerClass(); add.addActionListener(handler); subtract.addActionListener(handler); multiply.addActionListener(handler); divide.addActionListener(handler); ansbutton.addActionListener(handler); } public class HandlerClass implements ActionListener{ public double sum; public double fn; public double sn; public void actionPerformed(ActionEvent e) { if(add.isSelected()) operator = "+"; label.setText("+"); if(subtract.isSelected()) operator = "-"; label.setText("-"); if(multiply.isSelected()) operator = "x"; label.setText("x"); if(divide.isSelected()) operator = "/"; label.setText("/"); if(ansbutton.isSelected()) JOptionPane.showMessageDialog(null, "The answer is " + sum, "Answer", JOptionPane.PLAIN_MESSAGE); } } } {/ p>的价值

nil

func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { var destView: OtpVC = segue.destinationViewController as! OtpVC destView.dataPassed = "\(mobile)" } 是目标ViewController,mobile是我想传输到OtpVC视图的变量。 提前谢谢。

1 个答案:

答案 0 :(得分:3)

你在某处做错了什么。

如果您以编程方式推送/呈现下一个OtpVC,则不会调用prepareForSegue,因此destView.dataPassed将为nilprepareForSegue仅在情节提要中定义视图控制器转换时才有效,或者在代码中调用performSegue进行转换。

例如,推送:

if let navigationController = self.navigationController {
    let nextScreen = OtpVC()
    nextScreen.dataPassed = "\(mobile)"
    navigation.pushViewController(nextScreen)
}

或提出:

let nextScreen = OtpVC()
nextScreen.dataPassed = "\(mobile)"
self.presentViewController(nextScreen, animated: true, completion: nil)