自定义委托/协议没有正常工作



我一直在写一个应用程序,有自定义协议发送数据从子视图到父视图的类是

  • MainViewController
  • AddViewController(子视图控制器)
  • DaysViewController (addviewcontroller的子视图)

自定义协议在DaysViewController中声明并在AddViewController中实现

AddViewController.h

#import <UIKit/UIKit.h>
#import "DaysViewController.h"
#import "Course.h"
#import "Student.h"
@interface AddViewController : UITableViewController<UIActionSheetDelegate,UIPickerViewDelegate,UIPickerViewDataSource,UIPickerViewAccessibilityDelegate,UITextFieldDelegate,DaysViewControllerDelegate>
{
    NSArray *hoursarray;
    UIActionSheet *aac;
    IBOutlet UITextField *NameTx,*HoursTx,*DaysTx,*TimeTx;
    Student *st;
    Course *cc;
}
-(void) pickerDoneClick;
-(IBAction)fillTheOtherData;
@property (nonatomic ,strong) UIActionSheet *aac;
@end

AddViewController.m

#import "AddViewController.h"
@interface AddViewController ()
@end
@implementation AddViewController
@synthesize aac;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (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;
    //viewP.frame = CGRectMake(0, 154, 320, 205);
    hoursarray = [[NSArray alloc]initWithObjects:@"2",@"3",@"4", nil];
    [self.tableView setScrollEnabled:NO];
    [DaysTx setEnabled:NO];
    [TimeTx setEnabled:NO];
    [HoursTx setKeyboardType:UIKeyboardTypeDecimalPad];
    cc = [[Course alloc]init];
    //UITapGestureRecognizer *tapgr = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped:)];
    //[self.view addGestureRecognizer:tapgr];

}

-(void)tapped:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [hoursarray count];
}

//-(IBAction)addCourse
//{
//    [UIView beginAnimations:@"view" context:nil];
//    [UIView setAnimationDuration:1];
//    viewP.frame = CGRectMake(0, 500, 320, 205);
//    [UIView commitAnimations];
//
//}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [hoursarray objectAtIndex:row];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        if (indexPath.row == 3)
    {
        aac = [[UIActionSheet alloc]initWithTitle:@"How many ?" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
        UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 0, 0)];
        picker.delegate = self;
        picker.dataSource = self;
        UIToolbar *pickerToolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
        [pickerToolBar sizeToFit];
        NSMutableArray *barItems = [[NSMutableArray alloc]init];
        UIBarButtonItem *flexspace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        [barItems addObject:flexspace];
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClick)];
        [barItems addObject:doneBtn];
        [pickerToolBar setItems:barItems animated:YES];
        [aac addSubview:pickerToolBar];
        [aac addSubview:picker];
        [aac showInView:self.view];
        [aac setBounds:CGRectMake(0, 0, 320, 464)];

    }
    else if (indexPath.row == 2)
    {
        DaysViewController *days = [self.storyboard instantiateViewControllerWithIdentifier:@"Days"];
        days.delegate = self;
        [self.navigationController pushViewController:days animated:YES];
    }

}
-(void) pickerDoneClick
{
    [aac dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)chooseDays:(DaysViewController *)controller withArray:(NSArray *)theDaysArray
{
    NSLog(@"I'm @ chooseDays method");
    NSLog(@"Before the add !!");
    NSLog(@"chooseDays Method and the array is %@",theDaysArray);
    cc.days = [NSMutableArray arrayWithObject:theDaysArray];
    NSLog(@"After the add");
    NSLog(@"chooseDays Method and the array is %@",cc.days);
//    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)cancelChooseDays:(DaysViewController *)controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(IBAction)fillTheOtherData
{
    cc.name = NameTx.text;
    cc.hour = [HoursTx.text integerValue];
    NSLog(@"The name is %@ and the hour credit is %d",cc.name,cc.hour);
}
@end

DaysViewController.h

#import <UIKit/UIKit.h>
#import "Course.h"
@class DaysViewController;
@protocol DaysViewControllerDelegate <NSObject>
@required
-(void)chooseDays:(DaysViewController *)controller withArray:(NSArray *)theDaysArray;
-(void)cancelChooseDays:(DaysViewController *)controller;
@end

@interface DaysViewController : UITableViewController
{
    Course *courseDays;
    NSArray *days;
    NSMutableArray *dayChosen;
}
@property (nonatomic,weak) id<DaysViewControllerDelegate> delegate;
-(IBAction)done:(id)sender;
-(IBAction)cancel:(id)sender;
@end

DaysViewController.m

#import "DaysViewController.h"
@interface DaysViewController ()
@end
@implementation DaysViewController
@synthesize delegate;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (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;
    days = [[NSArray alloc]initWithObjects:@"Saturday",@"Sunday",@"Monday",@"Teusday",@"Wednesday",@"Thursday",@"Friday", nil];
    dayChosen = [[NSMutableArray alloc]init];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [days count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DaysCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DaysCell"];
    }
    cell.textLabel.text = [days objectAtIndex:indexPath.row];
    return cell;
}
-(IBAction)done:(id)sender
{
    //Course *course = [[Course alloc]init];
    //[course.days addObject:dayChosen];
    //NSArray *daysArray = [NSArray arrayWithObject:dayChosen];
    NSLog(@"The Days are %@",dayChosen);
    [self.delegate chooseDays:self withArray:dayChosen];
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(IBAction)cancel:(id)sender
{
    [self.delegate cancelChooseDays:self];
}
/*
// 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 - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    [dayChosen addObject:[days objectAtIndex:indexPath.row]];
    NSLog(@"Days are %@",dayChosen);
}
@end

MainViewController有一个按钮带我到AddViewController和AddViewController有相同的按钮带我到DaysViewController,所有的视图都有一个UITableView。

我想做的是当我把数据从DaysViewController发送到AddViewController把它放在一个数组中并解散视图AddViewController应该显示但是MainViewController显示这是我不想要的

AddViewController和DaysViewController有一个UINavigationController,但是MainViewController没有。

提前感谢。

好了,我想我知道你的问题是什么了。在cancel函数中,尝试切换这行代码:

[self dismissViewControllerAnimated:YES completion:nil];

用这行代码:

[self.navigationController popViewControllerAnimated:YES];

如果有帮助请告诉我。

最新更新