在表视图控制器

时间:2017-07-26 23:46:50

标签: ios objective-c uitableview delegates datasource

我正在尝试为应用程序创建一个设置页面,该应用程序使用表视图控制器传回数据,根据第二个表视图控制器的选定行的文本更改第一个视图控制器的详细文本标签。我在初始表视图控制器上将NSLog放在我的委托方法下,并且没有被调用。我很困难,非常感谢任何帮助!

初始VC .h:

//  EditAlarmTVC.h (Initial table view controller .h file)
//

#import <UIKit/UIKit.h>
#import "AlarmSoundTVC.h"

@interface EditAlarmTVC : UITableViewController <AlarmSoundDelegate>

@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;
@property (weak, nonatomic) IBOutlet UITableViewCell *offMethodCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *repeateCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *alarmLabelCell;
@property (weak, nonatomic) IBOutlet UITableViewCell *alarmSoundCell;
@property (strong) NSManagedObjectModel *alarm;

- (IBAction)cancelSetAlarm:(id)sender;
- (IBAction)saveSetAlarm:(id)sender;

@end

初始VC .m:

//EditAlarmTVC.m (Initial table view controller.m file)

#import "EditAlarmTVC.h"
#import <CoreData/CoreData.h>

@interface EditAlarmTVC ()

@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (retain, nonatomic) NSMutableArray *detailedTextLabels;

@end

@implementation EditAlarmTVC

@synthesize alarm;


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"offMethod"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"timePicker"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"repeateLabelSoundCell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"deleteAlarmCell"];
    self.detailedTextLabels =[NSMutableArray array];
    [self.detailedTextLabels addObject:@"Off"];
    [self.detailedTextLabels addObject:@"Never"];
    [self.detailedTextLabels addObject:@"Wake up, get up!"];
    [self.detailedTextLabels addObject:@"Default"];

}
-(void) viewWillAppear:(BOOL)animated{
    [self.offMethodCell.detailTextLabel setText:[self.detailedTextLabels objectAtIndex:0]];
    [self.repeateCell.detailTextLabel setText:[self.detailedTextLabels objectAtIndex:1]];
    [self.alarmLabelCell.detailTextLabel setText:[self.detailedTextLabels objectAtIndex:2]];
    [self.alarmSoundCell.detailTextLabel setText:[self.detailedTextLabels objectAtIndex:3]]; 
}

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

- (void)sendSelection:(NSString *)selectedAlarm{
      NSLog(@"hello"); 
    [self.detailedTextLabels replaceObjectAtIndex:3 withObject:selectedAlarm];
    [self.myTableView reloadData];
}

-(NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context =nil;
    id delegat = [[UIApplication sharedApplication] delegate];
    if ([delegat respondsToSelector:@selector(managedObjectContext)]) {
        context = [delegat managedObjectContext];
    }
    return context;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    if(indexPath.section ==2 & indexPath.row==0){
        [self performSegueWithIdentifier:@"repeateSegue" sender:indexPath];
    } else if (indexPath.section ==2 & indexPath.row==1){
        [self performSegueWithIdentifier:@"alarmLabelSegue" sender:indexPath];
    } else if (indexPath.section ==0 & indexPath.row==0){
        [self performSegueWithIdentifier:@"offMethodSegue" sender:indexPath];
    } else if (indexPath.section ==2 & indexPath.row==2){
        [self performSegueWithIdentifier:@"alarmSoundSegue" sender:indexPath];
    } else if (indexPath.section ==3 & indexPath.row==0){
        //DELETE ALARM
    NSManagedObjectContext *context = [self managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alarm" inManagedObjectContext:context];

    NSDateFormatter *timeFormatter = [[NSDateFormatter alloc]init];
    [timeFormatter setDateFormat:@"h:mm"];
    NSDate *timeNSDate = [self.datePicker date];
    NSString *timeString = [timeFormatter stringFromDate:timeNSDate];

    NSDateFormatter *amPmFormatter = [[NSDateFormatter alloc]init];
    [amPmFormatter setDateFormat:@"a"];
    NSDate *amPmNSDate = [self.datePicker date];
    NSString *amPmString = [amPmFormatter stringFromDate:amPmNSDate];



        NSPredicate *p1 = [NSPredicate predicateWithFormat:@"time == %@", timeString];
        NSPredicate *p2 = [NSPredicate predicateWithFormat:@"amPm == %@", amPmString];
        NSPredicate *p3 = [NSPredicate predicateWithFormat:@"offMethod == %@", self.offMethodCell.detailTextLabel.text];
        NSPredicate *p4 = [NSPredicate predicateWithFormat:@"repeate == %@", self.repeateCell.detailTextLabel.text];
        NSPredicate *p5 = [NSPredicate predicateWithFormat:@"alarmSound == %@", self.alarmSoundCell.detailTextLabel.text];

        NSPredicate *p6 = [NSPredicate predicateWithFormat:@"alarmLabel == %@", self.alarmLabelCell.detailTextLabel.text];


        NSPredicate *alarmsPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2, p3, p4, p5, p6]];

        [fetchRequest setEntity:entity];
        [fetchRequest setPredicate:alarmsPredicate];

        NSError *error;
        NSArray *items = [context executeFetchRequest:fetchRequest error:&error];

        for (NSManagedObject *managedObject in items)
        {
            [context deleteObject:managedObject];
        }

    EditAlarmTVC *goToEditAlarmTVC = [self.storyboard instantiateViewControllerWithIdentifier:@"setAlarmVC"];
    [self.navigationController pushViewController:goToEditAlarmTVC animated:YES];
    [self dismissViewControllerAnimated:NO completion:nil];
    } else
        return;
}

#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 {
    if ([segue.identifier isEqualToString:@"alarmSoundSegue"])
    {
        AlarmSoundTVC *alarmSoundTVC = [[AlarmSoundTVC alloc]init];
        [alarmSoundTVC setDelegate:self];
    }
}

@end

第二个VC .h:

//AlarmSoundTVC.h (Second table view controller .h file)

#import <UIKit/UIKit.h>

@protocol AlarmSoundDelegate <NSObject>
@required
-(void) sendSelection:(NSString *)selectedAlarm;
@end

@interface AlarmSoundTVC : UITableViewController 
@property (strong, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, weak) id <AlarmSoundDelegate> delegate;

@end

第二个VC .m:

// AlarmSoundTVC.m (Second table view controller .m file)

#import "AlarmSoundTVC.h"
#import "EditAlarmTVC.h"

@interface AlarmSoundTVC ()

@end

@implementation AlarmSoundTVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myTableView.delegate = self;
}

-(void) viewWillDisappear:(BOOL)animated{
    NSIndexPath *indexPath = [self.myTableView indexPathForSelectedRow];
    UITableViewCell *selectedCell =[self.myTableView cellForRowAtIndexPath:(indexPath)];
    NSString *selectedAlarm = selectedCell.textLabel.text;
    [self.delegate sendSelection:selectedAlarm] ;

}

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

#pragma mark - Table view data source

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [super tableView:tableView cellForRowAtIndexPath:indexPath];

}


#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 {
//    }

@end

如果那里有一些额外的不必要代码,我很抱歉。我有点新,想确保我提供了大量的信息。再次感谢!

1 个答案:

答案 0 :(得分:0)

prepareForSegue中,您要在新的本地AlarmSoundTVC实例上设置委托,该方法一退出就会被丢弃。您需要使用将要呈现的实例,可以通过segue的destinationViewController属性访问该实例:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"alarmSoundSegue"])
    {
        AlarmSoundTVC *alarmSoundTVC = (AlarmSoundTVC *)segue.destinationViewController;
        [alarmSoundTVC setDelegate:self];
    }
}