Best way to pass data from Child Modal VC to the Parent View Controller?

后端 未结 2 734
故里飘歌
故里飘歌 2021-02-04 13:19

What is the best way to pass data from a child modal view to the parent view controller?

I have a Child Modal Login Screen on my iPad app that I want to pass back user i

2条回答
  •  没有蜡笔的小新
    2021-02-04 14:12

    You can get it by using Protocol, it is Best way.

    I will give you the Basic Idea for how to create a Protocol

    Also, read this question: How do I create delegates in Objective-C?

    Following code give you the basic idea for Protocol, here in below code you can get Button title from MasterViewController to DetailViewController.

    #DetailViewController.h
    
    #import 
    
    @protocol MasterDelegate 
    -(void) getButtonTitile:(NSString *)btnTitle;
    @end
    
    
    @interface DetailViewController : MasterViewController
    
    @property (nonatomic, assign) id customDelegate; 
    
    #DetailViewController.m
    
    if([self.customDelegate respondsToSelector:@selector(getButtonTitile:)])
    {
              [self.customDelegate getButtonTitile:button.currentTitle];    
    }
    
    #MasterViewController.m
    
    create obj of DetailViewController
    
    DetailViewController *obj = [[DetailViewController alloc] init];
    obj.customDelegate = self;
    [self.navigationController pushViewController:reportTypeVC animated:YES];
    
    and add delegate method in MasterViewController.m for get button title.
    
    #pragma mark -
    #pragma mark - Custom Delegate  Method
    
    -(void) getButtonTitile:(NSString *)btnTitle;
    {
        NSLog(@"%@", btnTitle);
    
    }
    

提交回复
热议问题