Я новичок в Objective-C, и я пытаюсь понять делегатов. Я много искал и читал, но мне ничего не помогает. Я думаю, что лучший способ понять это может задавать вопрос с образцовым приложением.Делегат в Objective-C
Я пытаюсь создать приложение калькулятора оценки, чтобы проверить свои навыки. Вот мои файлы:
mainTableViewController.h
#import <UIKit/UIKit.h>
@interface mainTableViewController : UITableViewController
@end
mainTableViewController.m
#import "mainTableViewController.h"
#import "addLectureViewController.h"
@interface mainTableViewController()
@end
@implementation anaTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
[lectureName count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
return cell;
}
@end
addLectureViewController.h
#import <UIKit/UIKit.h>
@interface addLectureViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *lectureNameTextField;
- (IBAction)addButtonPressed:(id)sender;
@property NSMutableArray *lectureName;
@property NSObject *lecture;
@end
addLectureViewController.m
#import "addLectureViewController.h"
@interface addLectureViewController()
@end
@implementation addLectureViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_lectureName = [[NSMutableArray alloc] init];
_lecture = [[NSObject alloc] init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)addButtonPressed:(id)sender {
_lecture = _lectureNameTextField.text;
[_lectureName addObject:_lecture];
NSLog(@"%@",_lectureName);
}
@end
Все в порядке до сих пор. Но когда я пытаюсь использовать _lectureName
NSMutableArray
в mainTableViewController.m, я не вижу массив.
Я знаю коды для печати массива в таблицеView. Я знаю, что их сейчас нет. Я просто не понимаю, как реализовать код делегата для моего кода.
Почему вы указали код 'anaTableViewController', если он не используется нигде? – trojanfoe
но где делегаты? – HRM
Вы имеете в виду, что хотите передать данные ** _ lectureName ** на ** mainTableViewController ** ?? – iamyogish