在视图控制器之间有很多段可以吗?

时间:2016-03-08 01:35:15

标签: ios swift uiviewcontroller uitabbarcontroller

所以我对iOS开发很新,并决定冒险走捷径。我现在正在处理多个视图控制器并在两者之间传递数据,而我想知道的是,如果在两个视图控制器之间有多个(如9-15个)段,这是不好的做法吗?

基本上我所拥有的是具有9个不同按钮的普通VC。现在,我想要的是每个按钮将不同的数据发送到后续的选项卡式VC,而不是为每个按钮/类别提供不同的VC。

它看起来像这样:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    let tabBarController = segue.destinationViewController as! UITabBarController
    let svc = tabBarController.viewControllers![0] as! InfoViewController
    let svc2 = tabBarController.viewControllers![1] as! DIYViewController

    if segue.identifier == "wind" {
        svc.titleString = "Wind Power... what a powerful thing"

    } else if segue.identifier == "geothermal" {
        //send info about geothermal
    } else if segue.identifier == "hydroelectric" {
        //send infor about hydroelectricity
    } else if segue.identifier == "" {
        //code
    } else if segue.identifier == "" {
        //code
    } else if segue.identifier == "" {
        //code
    } else if segue.identifier == "" {
        //code
    }
}

有更传统的方法来实现这个目标吗?

2 个答案:

答案 0 :(得分:2)

您应该只有一个segue,并传递prepareForSegue(_:sender:)中的数据。例如。在第一个视图控制器中,传递变量:

class ViewController: UIViewController {
    var myVariable: String!
    @IBAction func button1Action(sender: AnyObject) {
        myVariable = "Hello"
    }
    @IBAction func button2Action(sender: AnyObject) {
        myVariable = "Hola"
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue == "MySegue" {
            let secondViewController = segue.destinationViewController as! ViewController
            secondViewController.myVariable = myVariable
        }
    }
}

在第二个视图控制器中,您可以使用变量执行任何操作:

class SecondViewController: UIViewController {
    var myVariable: String!

    override func viewDidLoad() {
        title = myVariable
    }
}

答案 1 :(得分:1)

您可以创建NSObject子类(例如MyAtmospherologyObject)并将所有需要的属性放入其中,以存储您可能需要的所有9个类别的所有信息。

然后给它一个类型属性并使用enum作为唯一的segue来确定它的内容以及它应该如何处理其余的数据。

在此之后,您只需要发送一个对象,即MyAtmospherologyObject。这可能很有用,因为您可以在初始化对象时将其作为一个整体传递给对象,并从那里重用它。

e.g。 Objective-C (我知道这不是Swift,但你明白了。)

<强> MyAtmospherologyObject.h

//
//  MyAtmospherologyObject.h
//

@import Foundation;

typedef NS_ENUM(NSUInteger, MyAtmospherologyType) {
    MyAtmospherologyGeothermalType   = 0,
    MyAtmospherologyHydroelectricType
};

@interface MyAtmospherologyObject : NSObject

@property (nonatomic)           MyAtmospherologyType type;
@property (nonatomic, strong)   NSString *mySting;

- (MyAtmospherologyType)type;

@end

<强> MyAtmospherologyObject.m

//
//  MyAtmospherologyObject.m
//

#import "MyAtmospherologyObject.h"

@implementation MyAtmospherologyObject

- (instancetype)initWithType:(MyAtmospherologyType)aType
{
    self = [super init];
    if (self) {
        _type = aType;
    }
    return self;
}

#pragma mark - NSKeyedArchiver

- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        _type       = [decoder decodeIntegerForKey:@"type"];
        _mySting    = [decoder decodeObjectForKey:@"mySting"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
    [encoder encodeInteger:_type    forKey:@"type"];
    [encoder encodeObject:_mySting  forKey:@"mySting"];
}

#pragma mark - Methods

- (MyAtmospherologyObject)type
{
    return _type;
}

@end

<强> MyAtmospherologyViewController.m

// ...

#import "MyAtmospherologyObject.h"

@implementation MyAtmospherologyViewController

// ...

- (IBAction)buttonAction:(id)sender
{
    MTKGSJournalQuestionnaireObject *object = [[MTKGSJournalQuestionnaireObject alloc] initWithType:MyAtmospherologyGeothermalType];
    object.myString = @"aString";
    [self performSegueWithIdentifier:@"MySegueIdentifier"
                              sender:object];
}

#pragma mark - UIStoryboardSegue

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    MyAtmospherologyObject *object = (MyAtmospherologyObject *)sender;
    MyAtmospherologySegueViewController *vc = (MyAtmospherologySegueViewController *)segue.destinationViewController;

    switch (object.type) {
        case MyAtmospherologyGeothermalType:
            vc.navigationItem.title = object.myString;
            break;
        case MyAtmospherologyHydroelectricType:
            vc.navigationItem.title = object.myString;
            break;
        default:
            break;
    }
}

// ...

@end