2014-10-28 91 views
0

這是我的情況。我使用NSTimer,計數15秒,但我也有按鈕,我用它來提前停止計時器。還有什麼更多的這個按鈕帶我到下一個tableViewController。第二個參數是日期 - 歐洲中心的當前時間。如果我點擊一個按鈕,我需要移動到下一個TableViewController,並將這兩個參數放到TableViewController中。這是我的代碼:如何將兩個參數傳遞給下一個ViewController?

**FirstViewController:** 

    import UIKit 

    class FirstViewController: UIViewController { 

     var timer = NSTimer() 
     @IBOutlet weak var labelCounter: UILabel! 


      override func viewDidLoad() { 
       super.viewDidLoad() 
       timer = NSTimer.scheduledTimerWithTimeInterval(1,target:self, selector: Selector("update"),userInfo: nil, repeats :true) 
      } 

      func update(){ 

        timer.invalidate() 
      } 

      override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) { 
     if (segue.identifier == "segueTest") { 
      var transfer = segue!.destinationViewController as SecondViewController 
      transfer.toPass = labelCounter.text 
      transfer.toPass2 = "\(NSDate())" 
     } 

    } 

      @IBAction func buttonPressed(button: UIButton){ 

       if labelCounter.text > "0" { 

        timer.invalidate() 

       } 


SecondViewController: 
import UIKit 

class ThirdViewController: UIViewController { 


    @IBOutlet weak var label1: UILabel! 
    @IBOutlet weak var label2: UILabel! 

    var toPass: String! 
    var toPass2: String! 


    override func viewDidLoad() { 
     super.viewDidLoad() 
    label1.text = toPass2 
    label2.text = toPass 
     // Do any additional setup after loading the view. 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

此代碼工作正常:)在故事板我必須連接firstViewController與SecondViewController。我通過連接我的按鈕與secondViewController並將segue標識符設置爲「segueTest」來做到這一點。

+1

可能重複查看控制器](http://stackoverflow.com/questions/5210535/passing-data-between-view-contro llers) – 2014-10-28 09:45:38

+0

你可以使用'self.performSegueWithIdentifier(「mySegue」,sender:self)''從'buttonPressed'調用segue。其餘的解釋在這裏 - 可能的重複[傳遞變量從一個ViewController到另一個在Swift](http://stackoverflow.com/questions/24044108/pass-variables-from-one-viewcontroller-to-another-in-swift ) – Paulw11 2014-10-28 09:48:56

+0

我解決了這個問題,我編輯了上面的代碼。 – Tannis 2014-10-28 15:07:17

回答

2

我不知道它有用或不適合你

BViewContr ol.h

@interface Showdeals : UIViewController 
    { 
     NSString *TotalJSonString; 
     NSString *PassingString; 
     NSMutablearray *setarray; 
    } 
    @property (strong,nonatomic)NSString *TotalJSonString; 
    @property (strong,nonatomic)NSString *PassingString; 

BViewControl.m

- (void)viewDidLoad 
{ 
[setarray addObject:TotalJSonString]; 
[setarray addObject:PassingString]; 
} 

最後你setarray傳遞給UITableView的

AViewControl.m 按鈕動作之間[傳遞數據的

BViewControl *show=[[BViewControl alloc]initWithNibName:nil bundle:nil]; 
    [show setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal]; 
    show.TotalJSonString=string1; 
    show.PassingString=string2; 

    [self.navigationController popToRootViewControllerAnimated:TRUE]; 
    [self presentViewController:show animated:YES completion:NULL ]; 
0

在secondview控制器中創建一個屬性。並使用類對象將值從當前視圖控制器分配給secondview控制器屬性。

0

覆蓋prepareForSegue

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    // Get the new view controller using segue.destinationViewController. 
    // Pass the selected object to the new view controller. 

    //CHECK SEGUE IDENTIFIER HERE IF YOU HAVE MULTIPLE SEGUES 
    (segue.destinationViewController as MyViewController).property = value // you can repeat this for any number of properties in your destination view controller 
} 
+0

我想這種傳遞數據的方法,所以如果我這樣做,我怎麼可以在界面生成器中連接兩個視圖? – Tannis 2014-10-28 10:22:32

+0

您在顯示視圖控制器的操作是什麼?如果是單擊某個按鈕或選擇一個tableview,則將該UI元素的一個segue連接到視圖控制器。 http://www.raywenderlich.com/5191/beginning-storyboards-in-ios-5-part-2 – 2014-10-28 11:04:04

0

你確實應該使用prepareForSegue。下面是代碼:

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

if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) { 
    [segue.destinationViewController performSelector:@selector(setMyData:) 
              withObject:myData]; 
    } 
} 

在目標視圖控制器,你應該有

@property (nonatomic, strong) MyData *myData; 

如果你想傳遞多個值,可以使一個對象,與這兩個值,然後傳遞一個對象作爲數據。或者,您可以創建一個數組,您可以在其中放置數據或字典,並將其作爲數據傳遞給您。

因此,邁德特可以的NSString,NSArray的,NSDictionary的,或者任何你需要:)

編輯:

有你可以做其他的事情,它完全不包括塞格斯。 您可以製作只存在於整個應用生命週期中的靜態變量。

我總是在所有的應用程序中做出的是一個常量類。

這裏是一些代碼:

Constants.h

@interface Constants : NSObject 

+ (Constants*)shared; 
@property NSString* constantA; 
@property int constantB; 
@property NsMutableArray array; 

@end 

Constants.m

@implementation Constants 

static Constants *constants = nil; 


+ (Constants*)shared { 

if (nil == constants) { 

    if (constants == nil) { 
     constants = [[[self class]alloc] init]; 
    } 
    // Remember to initialize an array (this is just an example, if you need an array. if you need only one int and string for example, skip this) 
    [Constants shared].array = [[NSMutableArray alloc]init]; 
    } 

return constants; 
} 

現在,讓我們說你有2個ViewControllers,和你想讀一些變量第一個,當你繼續到另一個。

第一的ViewController

,只要做到這一點:

[Constants shared].constantA = yourValue1; 
[Constants shared].constantB = yourValue2; 

然後,當你到了secont視圖控制器,讀取常量類你的價值觀是這樣的:

int  firstVal = [Constants shared].constantA; 
NSString* secondVal = [Constants shared].constantB; 

記住#IMPORT兩個ViewControllers中的「Constants.h」。

這在Swift中也很容易做到。我不知道它的語義,但我敢肯定,你自己看着辦吧:)

EDIT 2

如果變量之一必須是時間,這取決於時間當用戶按下按鈕時,可以執行以下操作(在本例中,將需要的時候,用戶在此格式按下按鈕:@「YYYY-MM-DD HH:MM:SS」

-(IBAction) onButtonClick 
{ 
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init]; 
[DateFormatter setDateFormat:@"yyyy-MM-dd%20HH:mm:ss"]; 
NSString dateTime = [DateFormatter stringFromDate:[NSDate date]]; 

[Constants shared].time = dateTime; 

// and after you do it, you can segue to another ViewController, like you do 
let view = self.storyboard?.instantiateViewControllerWithIdentifier("segue") as TableViewController 
self.navigationController?.pushViewController(view, animated: true) 

} 
+0

你可以在Swift中編寫這段代碼嗎? ; d對於NSArray中的「a」和「b」兩個對象? – Tannis 2014-10-28 10:41:14

+0

不幸的是,我不知道很快:( – SteBra 2014-10-28 10:43:43

+0

我做了一個編輯我的答案。有兩種方式來在兩個(和更多)ViewControllers之間傳輸數據uzing靜態變量 – SteBra 2014-10-28 12:00:10

相關問題