18 Mar 2017

UIPopoverPresentationController, Programme to show Popover ...


UIPopoverPresentationController, Programme to show Popover ...

StoryBoard:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)popoverBtn:(id)sender;
@property (weak, nonatomic) IBOutlet UIButton *popoverBtn;

@end

#import "ViewController.h"


@interface ViewController ()<UIPopoverPresentationControllerDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)popoverBtn:(id)sender {
    
    // grab the view controller we want to show
    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"Pop"];
    
    // configure the Popover presentation controller
    UIPopoverPresentationController *popController = [controller popoverPresentationController];
    popController.permittedArrowDirections = UIPopoverArrowDirectionAny;

    UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:controller];/*Here popController is controller you want to show in popover*/
    
    controller.preferredContentSize = CGSizeMake(200,200);
    destNav.modalPresentationStyle = UIModalPresentationPopover;
    popController = destNav.popoverPresentationController;
    popController.delegate = self;
    popController.sourceView = self.view;
    popController.sourceRect = self.popoverBtn.frame;
    destNav.navigationBarHidden = YES;
    [self presentViewController:destNav animated:YES completion:nil];

}

- (UIModalPresentationStyle) adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller {
    return UIModalPresentationNone;
}

- (void)dismissMe {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


# pragma mark - Popover Presentation Controller Delegate

- (void)popoverPresentationControllerDidDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
    
    // called when a Popover is dismissed
}

- (BOOL)popoverPresentationControllerShouldDismissPopover:(UIPopoverPresentationController *)popoverPresentationController {
    
    // return YES if the Popover should be dismissed
    // return NO if the Popover should not be dismissed
    return YES;
}

- (void)popoverPresentationController:(UIPopoverPresentationController *)popoverPresentationController willRepositionPopoverToRect:(inout CGRect *)rect inView:(inout UIView *__autoreleasing  _Nonnull *)view {
    
    // called when the Popover changes position
}
@end



#import <UIKit/UIKit.h>

@interface Pop : UIViewController

@end

#import "Pop.h"

@interface Pop ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation Pop

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(5, 5, 190,225)];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.separatorColor = [UIColor greenColor];
    [self.view addSubview:tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    NSString *sectionName;
    if (section == 0)
        sectionName = @"Section 0";
    
    if (section == 1)
        sectionName = @"Section 1";
    
    return sectionName;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger numOfRows = 0;
    numOfRows = section == 0 ? 15 : 10; //if else single line condition
    return numOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *MyIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:MyIdentifier] ;
    }
    @try {
        cell.textLabel.text = @"Pop tableView Cell";
        cell.backgroundColor = [UIColor purpleColor];
        cell.textLabel.textColor = [UIColor greenColor];
    }
    @catch (NSException *exception) {
    }
    @finally {
    }
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Table view didselectRowAtIndexPath method called");
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

Results: 

      

No comments:

Post a Comment

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0