![](/img/trans.png)
[英]Passing data from one view controller to another without using Segue
[英]Passing label data from one view controller to the other
在我的第一個視圖控制器中,標簽顯示了用戶輸入的方程式的答案。 當用戶轉到下一個視圖控制器時,我希望第二個視圖控制器中的標簽具有與第一個視圖控制器中接收的相同的數據。 我已經在互聯網上閱讀了許多傳遞數據的文章和問題(包括此處的SO)。 似乎沒有人屬於我的問題。
例如,我的第一個視圖控制器中有2個文本字段和一個標簽。 如果用戶在兩個字段中都輸入數字2,則標簽將顯示4作為答案。 然后,我希望當用戶轉到下一個深入解釋該答案的視圖控制器時,標簽中顯示的答案可用。
編輯:我將這個放在我的項目中,這樣我可以更好地理解它。 我非常感謝所有回復!
在我的第一個名為“ View Controller”的視圖控制器中,用戶計算其BMI,該BMI顯示在稱為“ bmiView”的標簽中。
在我的第二個視圖控制器(TipsViewController)中,它顯示了用戶的分類。 例如,如果在用戶中,從第一個視圖控制器計算出的BMI為30.00,則將其歸類為“肥胖”,我想在第二個視圖控制器中顯示。
在FirstViewController中,假設您有一個UILabel *myLabel
myLabel.text =@"Hari kishan ";
SecondviewController ,第二個視圖控制器中還有另一個標簽。
@property(nonatomic, retain)NSString *secondLabel;
@property(nonatomic, retain)NSString *secondLabelText; //a string to contain text for that Label
//To set text of secondLabel as mylabel
//do this in **FirstViewController.m**
SecondvViewController *objSecondviewController =[[SecondviewController alloc]init];
objSecondviewController.secondLabelText=myLabel.text;
[self.navigationController pushViewController:objSecondviewController animated:YES];
現在在SecondvViewController.m中
self.secondLabel.text=secondLabelText;
FirstController
-(void)calculateAnswer{
int answer=[self.firstInput.text intValue] + self.secondInput.text intValue];
SecondController *controller=[[SecondController alloc] initWithNibName:@"SecondView" bundle:nil];
//Answer is a PUBLIC property declared in SecondController
controller.answer=[NSString stringWithFormat:@"%d",answer];
[self.navigationController pushViewController:controller animated:YES];
}
SecondController
在SecondController.h中
@property(nonatomic, retain)NSString *answer;
在SecondController.m中
-(void)viewDidLoad{
[super viewDidLoad];
self.lblAnswer.text=self.answer;
}
在視圖控制器2中使字符串的屬性
@property (nonatomic, strong) NSString * resultValue;
在viewdidLoad中
[self.textfieldOutlet setText:self.resultValue];
初始化2nd View控制器時
Viewcontroller2 *val=[Viewcontroller2 alloc ]init];
val.resultValue = self.resulttextFieldoutletinVC1.text;
推送/模態化視圖控制器
因此,只需將值傳遞到字符串中,即可在第二個視圖控制器中獲取值,並對其執行任何邏輯
您可以在secondView中創建字符串的屬性
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController {
}
@property (nonatomic, strong) NSString *strResultedText;
@end
這樣您就可以在推送或展示之前將標簽的值從第一個視圖傳遞到第二個視圖。 您可以使用SecondView的對象訪問屬性,例如:
SecondViewController *obj = //your initialization
obj.strResultedText = lbl.text;
1)在Next View Controller中創建名為final
屬性:
@property(nonatomic, strong) NSString *final;
2)計算總和:
int firstVal = [yourFirstTextField.text intValue];
int secondVal = [yourSecondTextField.text intValue];
int resultVal = firstVal + secondVal;
NSString *final = [NSString stringWithFormat:@"%d",resultVal];
3)將其發送到Next View Controller:
NextViewController *navController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
nextViewObj.final = final;
[self.navigationController pushViewController:navController animated:YES];
4)在Next View Controller中:
yourLabel.text = self.final;
創建NSString
名為labelText
你SecondViewController.h
,讓您的標簽名稱FirstViewController
是myLabel
然后做你的“FirstViewController”:
SecondViewController *view = [[SecondViewController alloc] init];
view.labelText = myLabel.text;
您可以使用NSUserDefaults。 在First View Controller中,如果更改了標簽值,則可以保存值:
[[NSUserDefaults standardUserDefaults] setObject: label.text
forKey: @"lableValue"];
在第二個視圖控制器中:
- (void) viewDidAppear: (BOOL) animated
{
label.text = [[NSUserDefaults standardUserDefaults] stringForKey: @"lableValue"];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.