[ios] 프로그래밍 방식으로 Segue를 수행하고 매개 변수를 대상보기로 전달

내 응용 프로그램에는 프로그래밍 방식으로 segue를 수행하는 버튼이 있습니다.

- (void)myButtonMethod
{
    //execute segue programmatically
    [self performSegueWithIdentifier: @"MySegue" sender: self];
}

대상보기를 참조하고 일부 매개 변수를 전달하는 방법이 있는지 알고 싶습니다.

나는 prepareForSegue방법에서 그것을 참조 할 수 있다는 것을 알고 myDestinationViewController *vc = [segue destinationViewController];있지만, 프로그래밍 방식으로 segue를 실행하는 방법을 모르겠습니다.

당신은 어떤 아이디어가 있습니까?

감사합니다, yassa


최신 정보:

이 질문에 대해 죄송합니다 !!! segue가 프로그래밍 방식으로 호출 되더라도 prepareForSegue메소드가 어쨌든 호출되므로 동일한 일반적인 방식으로 매개 변수를 전달할 수 있음을 발견했습니다.



답변

답은 단순히 segue가 어떻게 트리거되는지 차이가 ​​없다는 것입니다.

prepareForSegue:sender:메소드는 어떤 경우에도 호출되며 여기에서 매개 변수를 전달합니다.


답변

오래된 질문이지만 여기에 원하는 것을 수행하는 방법에 대한 코드가 있습니다. 이 경우 테이블 뷰의 선택된 셀에서 다른 뷰 컨트롤러로 데이터를 전달합니다.

trget 뷰의 .h 파일에서 :

@property(weak, nonatomic)  NSObject* dataModel;

.m 파일에서 :

@synthesize dataModel;

dataModel할 수있다 string, int또는이 경우처럼 많은 항목이 포함 된 모델입니다

- (void)someMethod {
     [self performSegueWithIdentifier:@"loginMainSegue" sender:self];
 }

또는…

- (void)someMethod {
    UIViewController *myController = [self.storyboard instantiateViewControllerWithIdentifier:@"HomeController"];
    [self.navigationController pushViewController: myController animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"storyDetailsSegway"]) {
        UITableViewCell *cell = (UITableViewCell *) sender;
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        NSDictionary *storiesDict =[topStories objectAtIndex:[indexPath row]];
        StoryModel *storyModel = [[StoryModel alloc] init];
        storyModel = storiesDict;
        StoryDetails *controller = (StoryDetails *)segue.destinationViewController;
        controller.dataModel= storyModel;
    }
}


답변

스위프트 4 :

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "ExampleSegueIdentifier" {
        if let destinationVC = segue.destination as? ExampleSegueVC {
            destinationVC.exampleString = "Example"
        }
    }
}

스위프트 3 :

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "ExampleSegueIdentifier" {
            if let destinationVC = segue.destinationViewController as? ExampleSegueVC {
                destinationVC.exampleString = "Example"
            }
        }
    }


답변

한곳에서 segue를 수행하고 segue 준비를 위해 매개 변수를 보내도록 상태를 유지하는 문제를 이해합니다.

나는 이것을하는 방법을 알아 냈습니다. 범주를 사용하여 userInfoDict라는 속성을 ViewControllers에 추가했습니다. 그리고 나는 보낸 사람이 스스로 (컨트롤러 자체를 의미하는) 방식으로 식별자로 segue를 수행하는 것을 무시했습니다. 이 userInfoDict를 다음 ViewController에 전달합니다.

여기서 전체 UserInfoDict를 전달하는 대신 특정 매개 변수를 발신자로 전달하고 그에 따라 재정의 할 수도 있습니다.

명심해야 할 1 가지. ur performSegue 메소드에서 수퍼 메소드를 호출하는 것을 잊지 마십시오.


답변

새로운 빠른 버전을 사용하는 경우.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "ChannelMoreSegue" {

        }
}


답변