关闭

iOS PAD编写 POPOver 使用tableviewcontroller的两种操作方式,代理和消息通知

标签: ios
501人阅读 评论(0) 收藏 举报
分类:

viewcontroller:

//

//  ViewController.m

//  0710ipad01

//

//  Created by tianshangrenjian on 15/7/10.

//  Copyright © 2015 tianshangrenjian. All rights reserved.

//


#import "ViewController.h"

#import "SCHTableViewController.h"

#import "SCHNotifyTableViewController.h"

@interface ViewController ()<showFuck>

- (IBAction)menuBt:(id)sender;

- (IBAction)btnfuck:(id)sender;

@property (weak, nonatomic) IBOutlet UIButton *btOlNoti;

- (IBAction)btNotfi:(id)sender;

@property (strong ,nonatomic) SCHNotifyTableViewController *ntVc;

@property (strong ,nonatomic)NSNotificationCenter *ncf;

@property (weak, nonatomic) IBOutlet UIBarButtonItem *btnView1;


@property (weak, nonatomic) IBOutlet UIButton *btnView2;

@property (strong,nonatomic) UIPopoverController *popVc;

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    

}

- (void)showFuck:(NSNotification *)note

{

    NSString *str=note.userInfo[@"color"];

    NSLog(@"%@",str);

    if([str isEqualToString:@"red"]) {

        self.view.backgroundColor=[UIColor redColor];

    }

    else

    {

        if([str isEqualToString:@"blue"]) {

            self.view.backgroundColor=[UIColor blueColor];

        }

        else

        {

            self.view.backgroundColor=[UIColor greenColor];

            

        }

    }


}

- (void)dealloc

{

    [self.ncf removeObserver:self];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)menuBt:(id)sender {

    UIViewController *vc=[[UIViewController alloc] init];

    vc.view.frame=CGRectMake(0, 0, 200, 200);

    vc.view.backgroundColor=[UIColor blueColor];

    

    UIPopoverController *popVc=[[UIPopoverController alloc] initWithContentViewController:vc];

    [popVc presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];


}

- (void)showInfoInfView:(SCHTableViewController *)vc

{

    if([vc.str isEqualToString:@"red"]) {

        self.view.backgroundColor=[UIColor redColor];

    }

    else

    {

        if([vc.str isEqualToString:@"blue"]) {

            self.view.backgroundColor=[UIColor blueColor];

        }

        else

        {

            self.view.backgroundColor=[UIColor greenColor];


        }

    }

}

- (IBAction)btnfuck:(id)sender {

    SCHTableViewController *vc=[[SCHTableViewController alloc] init];

    vc.view.frame=CGRectMake(0, 0, 200, 200);

    vc.view.backgroundColor=[UIColor redColor];

    vc.delegate=self;

//    vc.preferredContentSize=CGSizeMake(300, 300);

    UIPopoverController *popVc=[[UIPopoverController alloc] initWithContentViewController:vc];

    popVc.backgroundColor=[UIColor greenColor];

    [popVc presentPopoverFromRect:self.btnView2.bounds inView:self.btnView2 permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];

    


}



- (IBAction)btNotfi:(id)sender {

    

    

    SCHNotifyTableViewController *vc=[[SCHNotifyTableViewController alloc] init];

    vc.view.backgroundColor=[UIColor redColor];

    self.ntVc = vc;

  

    UIPopoverController *popVc=[[UIPopoverController alloc ] initWithContentViewController:vc];

    popVc.backgroundColor=[UIColor blueColor];

    [popVc presentPopoverFromRect:self.btOlNoti.bounds inView:self.btOlNoti permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];

    

    self.ncf=[NSNotificationCenter defaultCenter];

    //addObserver:self消息观察者

    //objectself.ntVc消息发送者

    [self.ncf addObserver:self selector:@selector(showFuck:) name:nil object:self.ntVc];

   // [self.ncf addObserver:self.ntVc selector:@selector(showFuck:) name:@"color1" object:self];

}

@end


//代理方式

。h

//

//  SCHTableViewController.h

//  0710ipad01

//

//  Created by tianshangrenjian on 15/7/10.

//  Copyright © 2015 tianshangrenjian. All rights reserved.

//


#import <UIKit/UIKit.h>

@class SCHTableViewController;

@protocol showFuck <NSObject>


- (void)showInfoInfView:(SCHTableViewController *)vc;


@end

@interface SCHTableViewController : UITableViewController

@property (strong,nonatomic) id<showFuck> delegate;

@property (strong,nonatomic) NSString *str;

@end


.m文件

//

//  SCHTableViewController.m

//  0710ipad01

//

//  Created by tianshangrenjian on 15/7/10.

//  Copyright © 2015 tianshangrenjian. All rights reserved.

//


#import "SCHTableViewController.h"


@interface SCHTableViewController ()

@property (strong, nonatomic) NSMutableArray *colors;

@end


@implementation SCHTableViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.colors=@[@"red",@"blue",@"green"];

    self.preferredContentSize=CGSizeMake(100, self.colors.count*44);

    

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"color"];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Incomplete implementation, return the number of sections

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

#warning Incomplete implementation, return the number of rows

    return 3;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"color" forIndexPath:indexPath];

    if (cell==nil) {

        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"color"];

    }

    // Configure the cell...

    cell.textLabel.text=self.colors[indexPath.row];

    return cell;

}

- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath

{

    self.str=self.colors[indexPath.row];

    [self.delegate showInfoInfView:self];

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)tableView:(nonnull UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath

{

    NSLog(@"fuck:%@",self.colors[indexPath.row]);

    [self dismissViewControllerAnimated:YES completion:nil];

}

/*

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return NO if you do not want the specified item to be editable.

    return YES;

}

*/


/*

// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

    }   

}

*/


/*

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

}

*/


/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return NO if you do not want the item to be re-orderable.

    return YES;

}

*/


/*

#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


消息通知方式:

//

//  SCHNotifyTableViewController.m

//  0710ipad01

//

//  Created by tianshangrenjian on 15/7/10.

//  Copyright © 2015 tianshangrenjian. All rights reserved.

//


#import "SCHNotifyTableViewController.h"


@interface SCHNotifyTableViewController ()

@property (strong, nonatomic) NSMutableArray *colors;

@end


@implementation SCHNotifyTableViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    // Uncomment the following line to preserve selection between presentations.

    // self.clearsSelectionOnViewWillAppear = NO;

    

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.

    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    

    self.colors=@[@"red",@"blue",@"green"];

    

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"color1"];

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


#pragma mark - Table view data source


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

#warning Incomplete implementation, return the number of sections

    return 1;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

#warning Incomplete implementation, return the number of rows

    return 3;

}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"color1" forIndexPath:indexPath];

    

    // Configure the cell...

    

    if (cell==nil) {

        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"color1"];

    }

    cell.textLabel.text=self.colors[indexPath.row];

    

    return cell;

}


- (void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath

{

    NSString *str=self.colors[indexPath.row];

    NSNotificationCenter *nfc=[NSNotificationCenter defaultCenter];

    

    [nfc postNotificationName:@"color1" object:self userInfo:@{@"color":str}];

    [self dismissViewControllerAnimated:YES completion:nil];

}

/*

// Override to support conditional editing of the table view.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return NO if you do not want the specified item to be editable.

    return YES;

}

*/


/*

// Override to support editing the table view.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        // Delete the row from the data source

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    } else if (editingStyle == UITableViewCellEditingStyleInsert) {

        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view

    }   

}

*/


/*

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

}

*/


/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

    // Return NO if you do not want the item to be re-orderable.

    return YES;

}

*/


/*

#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






0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:207616次
    • 积分:4235
    • 等级:
    • 排名:第7212名
    • 原创:223篇
    • 转载:32篇
    • 译文:0篇
    • 评论:31条
    文章分类
    最新评论