swift 如何将标签内的标签传递给另一个视图控制器

How to pass label inside a label to another view controller in swift

我有一个视图,它包含一个可以更改的标签 dynamically.How 我可以将标签中的文本放入数组中,然后将其传递给另一个视图控制器吗?

创建一个类似 NSDictionary 或 NSArray 的数据结构,并将视图 A 的详细信息保存到其中。然后当您的视图 B 被调用时,将这些值传递给视图 B 并使用它来填充您的 TableView

swift

中的例子
  • ViewController Class

class ViewController: UIViewController {

var label1 = UILabel()
var label2 = UILabel()

override func viewDidLoad() {
    super.viewDidLoad()

  // Do any additional setup after loading the view, typically from a nib.

}

func updateLabels()
{

    label1.text = "LABEL 1"
    label2.text = "LABEL 2"
}


///If you are using Xibs

@IBAction func loadTableViewController()
{
    // Create a data structure for the data you want to send
    var array : NSMutableArray = NSMutableArray()

    array.addObject(label1.text!)
    array.addObject(label2.text!)


     // Create a viewcontroller  instance of tableViewController
    let viewcontroller = tableViewController(nibName:"tableViewController",bundle:nil)

    viewcontroller.dataSourceArray = array


}

//// If you are using Story Board
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {

    // Create a data structure for the data you want to send
    var array : NSMutableArray = NSMutableArray()

    array.addObject(label1.text!)
    array.addObject(label2.text!)

    // Create a new variable to store the instance of tableViewController
    let destinationVC = segue.destinationViewController as! tableViewController


    destinationVC.dataSourceArray = array
}
  • 表格视图控制器class

class 表ViewController: UITableViewController {

@IBOutlet var tableview: UITableView!
var dataSourceArray : NSMutableArray!


override func viewDidLoad() {
    super.viewDidLoad()
            // Do any additional setup after loading the view.

    // dataSourceArray will contain the label details passed from previous view controller.


}



override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


    // you can set the details from the dataSourceArray

}



override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    // you can set the details from the dataSourceArray
}

嗯,您的问题不是很清楚,但我可以预测,您正在从标签视图导航到 table 视图,并希望在 table 视图中显示标签的所有值。 您可以通过将这些标签的所有值放在一个 mutable 数组中并在 table 视图控制器中创建一个 属性 类型的 NSMutable 数组来实现这一点。并在导航时,将标签视图数组放入 table 视图控制器的 属性。 您可以参考下面的代码。

标签视图控制器

@interface DummyViewController (){
    NSMutableArray *array;
}

@end

@implementation DummyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"segue"]) {

        array = [[NSMutableArray alloc]initWithObjects:label1.text,label2.text,label3.text nil];

        FinalTableViewController *view = [self.storyboard instantiateViewControllerWithIdentifier:@"view"];
        view.dataArray = array;
    }

}

在table视图控制器中

.h 文件

@interface DummyViewController : UIViewController

     @property (nonatomic,strong) NSMutableArray *dataArray;

@end

现在在 table 视图委托方法中使用此数据数组来填充数据。 如果您觉得答案有用,请不要忘记打绿色勾号。