委托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 NSMutableArraymainTableViewController.m时,看不到该数组。

我知道在tableView中打印数组的代码。我知道他们现在不在那儿。我只是不了解我的代码的实现委托代码。

苏德山沙斯特里

如果要在表的行上显示某些内容,则可以使用NSArray,并且必须在委托方法中返回数组的计数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
 return array.count;
}

否则,表格将不会显示任何元素。而且,仅当您要在numberOfRowsInSection方法中返回特定数量的数组count时,才会调用委托方法cellForRowAtIndexPath。

您可以从以下链接获取参考以了解代表:http : //www.tutorialspoint.com/ios/ios_delegates.htm

如何设置一个简单的委托以在两个视图控制器之间进行通信?

http://code.tutsplus.com/tutorials/ios-sdk-custom-delegates--mobile-10848

但是对于tableView,委托方法是在内部定义并在内部触发的。我们只需要将这些委托设置为充当侦听器的控制器即可。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章