1. 程式人生 > >屬性傳值(一個頁面切換到下一個頁面, 值的傳遞)

屬性傳值(一個頁面切換到下一個頁面, 值的傳遞)

FirstViewController *firstVC = [[FirstViewController alloc] init];
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:firstVC];
    //半透明度預設為 no
    navigation.navigationBar.translucent = YES;
    self.window.rootViewController = navigation;
    [firstVC release];
    [navigation release];

    //傳值方式
//1.屬性傳值: 從前一個頁面向後一個頁面傳值 //a.在後一個頁面, 根據傳值的型別和個數, 寫屬性 //b.在前一個頁面, 為屬性賦值 //c.在後一個頁面, 使用值 //UIViewController的生命週期(一個物件從建立到消亡的過程) //1.alloc + init //2.loadView //3.viewDidLoad //4.viewWillAppear //5.viewDidAppear //6.view WillDisapper //7.viewDidDisapper //8.dealloc //注:1238, 只會執行一次; 4567:執行多次
return YES; } //在第二個頁面的操作: //傳值第一步 @property (nonatomic, copy)NSString *youName, *herName; //在第一個頁面的操作: - (void)test { if (yourTF.text.length == 0 || herTF.text.length == 0) { //給一個提示 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"名字不能為空" delegate:nil cancelButtonTitle:@"OK"
otherButtonTitles: nil]; [alertView show]; return; } // SecondViewController *secondController = [[SecondViewController alloc] init]; // //傳值第二步 // secondController.youName = yourTF.text; // secondController.herName = herTF.text; // [self.navigationController pushViewController:secondController animated:NO]; // [secondController release]; //模態切換 SecondViewController *secondVC = [[SecondViewController alloc] init]; secondVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self presentViewController:secondVC animated:YES completion:nil]; } //傳值第三步的操作: - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%s", __FUNCTION__); // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor cyanColor]; NSArray *array = @[@"你們的愛情基礎非常深厚,牢不可破,然而卻因為求學或工作的原因,使得你們分隔兩地,聚少離多。也許是因為相聚短暫而益顯珍貴,而e信來往,電話傳情給你們平添一份情調。", @" 你們是眾人眼中的神仙眷侶,站在一起怎麼看怎麼般配,很有傳說中的夫妻相。你們之間或許沒有驚天地泣鬼神的愛情神話,或許沒有刻骨銘心的激情,但是你的之間的溫情和默契卻能讓你們的心越走越近,一起慢慢變老。", @"你們是如此的匹配,如此和諧,往往是你剛想說“可惜”他已經開始嘆惜。你們性格相合,有共同的愛好,品味一致。就是這麼默契,令人們羨慕得不得了。沒辦法,誰叫你們連姓名的筆畫也一樣呢。", @"你們倆的姻緣線詭異,連鄙人也無法測算。", @" 你們的感情一般是從學生時代開始的,那青澀的戀愛留給你們酸酸甜甜的味道讓你們用一生去懷念彼此,但是你們的愛情卻註定了沒有結果,你們會試著遺忘對方,開始自己新的生活,但心中那最溫柔的角落一直被初戀佔據。", @"你們前世是冤家,今生做情侶,所以就難免有一些不和諧的音符出現來折磨你們的愛情。你們的感情大起大落,分分合合,不過你們最終會修成正果,恩愛到老的。"]; NSString *string = [NSString stringWithFormat:@"%@和%@的測試結果:\n%@", _youName, _herName, array[arc4random() % 6]];//對任何數取餘, 最終的結果:0至該數-1 //計算高度 //引數1: 容器大小, 必須保證其中一個固定, 不確定就寫0 //引數2: 計算方式, 行間距 + 行高 //引數3: 字型樣式 //引數4: 上下文, 用於傳值 NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]}; CGRect rect = [string boundingRectWithSize:CGSizeMake(335, 0) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dic context: nil];//最終只要 rect 的高度 //注意, 這樣寫的目的是為了, label 最終的高度與文字相同. 字串有個方法 boundingRectWithSize. //字典,尺寸大小; 基本上是寫在一塊的; //位運算(二進位制運算), 位運算, 支援, 依次取多個(只要通過按位或即可) //按位或:(|): 同0為0, 有一個是1, 即為1 //按位與:(&): 同1為1, 有一個是0, 即為0 //按位異或:(^): 相同為0, 不同為1 //按位取反(): 1變0, 0變1 //左移(<<):左移 n 位, 不夠的補0 //右移(>>)右移 n 位, 多餘的除去 UILabel *resultLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 84, 335, rect.size.height)]; resultLabel.text = string; resultLabel.numberOfLines = 0; resultLabel.backgroundColor = [UIColor whiteColor]; [self.view addSubview:resultLabel]; }