การส่งข้อมูลไปยังตัวควบคุมมุมมองที่แตกต่างกันเมื่อมีการนำเสนอตัวควบคุมแบบ modally?

ฉันค่อนข้างใหม่กับอุปกรณ์ iOS และคิดว่าฉันเข้าใจการส่งข้อมูลไปยังตัวควบคุมมุมมอง ชัดเจนว่าไม่. ฉันกำลังพยายามส่งข้อมูลภาพจาก ViewController หลักไปยัง ViewController ลูกที่แสดงแบบโมฆะ ฉันคิดว่าปัญหามีบางอย่างเกี่ยวกับวิธีการส่งวัตถุไปยังตัวควบคุมมุมมองที่นำเสนอแบบโมเดอเรเตอร์แทนที่จะส่งไปยังสแต็กการนำทาง ด้านล่างนี้เป็นรหัสจากผู้ปกครองและลูกที่เกี่ยวข้องกับปัญหาที่ฉันมี

จาก Parent.m:

- (IBAction)sort:(id)sender {
    SortSelectViewController *ssvc = [self.storyboard instantiateViewControllerWithIdentifier:@"sort"];
    ssvc.backgroundImage = [self screenshot];
    [self performSegueWithIdentifier:@"sort" sender:self];
    //The segue is linked from my parent view controller to my child with the identifier "sort"

}

- (UIImage *) screenshot {
    CGRect rect = CGRectMake(0, 20, 640, 1116);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, [UIScreen mainScreen].scale);

    [self.view drawViewHierarchyInRect:rect afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    return image;
}

นี่คือใน Child.h ของฉัน

@property (nonatomic, strong) UIImage *backgroundImage;

นี่คือลูกของฉัน.m:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"%@", self.backgroundImage);
    //This log will return nil when the program is run - clear sign data is not being correctly passed
    self.backgroundImage = [self.backgroundImage applyDarkEffect];
    self.imageView.image = self.backgroundImage;
}

ฉันได้นำเข้าตัวควบคุมมุมมองลูกของฉันไปยังคลาสหลักของฉันแล้ว


person smallTeamCraft Development    schedule 17.01.2014    source แหล่งที่มา
comment
ความเป็นไปได้ที่ซ้ำกันของ ฉันจะส่งข้อมูลระหว่างลำดับของกระดานเรื่องราวได้อย่างไร   -  person Midhun MP    schedule 17.01.2014
comment
ลองนำรูปภาพไปไว้ในผู้รับมอบสิทธิ์ของแอปและกำหนดรูปภาพในตัวควบคุมมุมมองลูกในวิธี viewwillapper   -  person Charan Giri    schedule 17.01.2014


คำตอบ (2)


ลองแบบนี้ โดยใช้ Story Bord ต้องใช้สองวิธีรวมกัน performSegueWithIdentifier: *prepareForSegue:*

    - (IBAction)sort:(id)sender {

        [self performSegueWithIdentifier:@"sort" sender:self];
        //The segue is linked from my parent view controller to my child with the identifier "sort"

    }



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

        if ([[segue identifier] isEqualToString:@"sort"])
        {
           SortSelectViewController *ssvc = [segue destinationViewController];
        ssvc.backgroundImage = [self screenshot];
        }
     }

- (UIImage *) screenshot {
       //return normal image 
    return [UIImage imagenamed:"imagename.png"];
}
person Muralikrishna    schedule 17.01.2014
comment
แก้ไข: ขออภัยฉันไม่ได้เปลี่ยนวิธีการเริ่มต้นตัวควบคุมมุมมองเป็นอันนั้น ใช้งานได้ แต่ทำไมฉันถึงไม่สามารถทำได้ทั้งหมดจากการกดปุ่ม? - person smallTeamCraft Development; 17.01.2014

รหัสของคุณ (คำถามเดิม) กำลังเชื่อมโยงการสร้างอินสแตนซ์โดยตรงของตัวควบคุมมุมมองและลำดับกระดานเรื่องราว เมื่อคุณใช้กระดานเรื่องราวที่แยกออกมา VC จะถูกสร้างเพื่อคุณโดยอัตโนมัติ คุณเข้าถึงมันเป็น "ตัวควบคุมมุมมองปลายทาง" ในส่วนต่อในวิธี "prepareForSegue:"

สิ่งที่เกิดขึ้นคือคุณสร้างอินสแตนซ์ของตัวควบคุมมุมมอง ตั้งค่ารูปภาพ จากนั้น... รูปภาพจะถูกโยนทิ้งไปและถูกละเว้น (vc ทั้งหมด)

จากนั้น คุณเริ่มต้นการทำต่อ ซึ่ง iOS จัดการโดยการสร้างอินสแตนซ์ อีก ซึ่งเป็นตัวควบคุมมุมมองที่แตกต่างกัน นี่คือตัวควบคุมมุมมองที่ iOS นำเสนอ

ดังนั้น: หากคุณกำลังจะใช้ Storyboard ที่ทำต่อ iOS จะสร้าง VC ให้คุณ อย่าสร้างอินสแตนซ์ขึ้นมาเอง เพราะไม่มีทางที่จะเชื่อมโยงอันที่คุณสร้างอินสแตนซ์เข้ากับการจัดการ iOS ต่อไปได้ สิ่งที่คุณยกตัวอย่างเป็นสิ่งพิเศษที่ไม่เกี่ยวข้องซึ่งจะถูกละเลย

person Bill Patterson    schedule 17.01.2014
comment
สุดยอด. คำอธิบายที่ดี นั่นคือสิ่งที่ฉันกำลังมองหา! - person smallTeamCraft Development; 17.01.2014