0

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

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

自定义协议在 DaysViewController 中声明并在 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

添加视图控制器.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 没有。

先感谢您。

4

1 回答 1

1

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

[self dismissViewControllerAnimated:YES completion:nil];

使用这行代码:

[self.navigationController popViewControllerAnimated:YES];

让我知道这是否有帮助。

于 2013-04-17T10:42:59.453 回答