繁体   English   中英

在Objective-C中代表

[英]Delegate in 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

到目前为止一切都还好。 但是当我尝试在mainTableViewController.m中使用_lectureName NSMutableArray时,我看不到数组。

我知道在tableView中打印数组的代码。 我知道他们现在不在那里。 我只是不明白实现委托代码到我的代码。

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

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

否则,表格不会显示任何元素。 只有在numberOfRowsInSection方法中返回特定数量的数组计数时,才会调用委托方法cellForRowAtIndexPath。

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

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

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

但是在tableView的情况下,委托方法在内部定义并在内部触发。 我们只需要将这些委托设置为充当监听器的控制器。

以下代码可能存在语法错误,但它们可以提供此代码的委托摘要。

进行以下更改: -

mainTableViewController.h

#import <UIKit/UIKit.h>

@interface mainTableViewController : UITableViewController
@property(strong, nonatomic) NSMutableArray *lectureName
@end

合成mainTableViewController.m中的属性lectureName

然后在addLectureViewController.h中进行以下更改

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

@interface addLectureViewController : UIViewController 
@property (weak, nonatomic) IBOutlet UITextField *lectureNameTextField;
- (IBAction)addButtonPressed:(id)sender;
@property(weak, nonatomic) id<mainTableViewController> delegate;
@property NSMutableArray *lectureName; 
@property NSObject *lecture;

@end

在addLectureViewController.m中合成属性委托

然后进行以下更改: -

- (IBAction)addButtonPressed:(id)sender {

    _lecture = _lectureNameTextField.text;
    [_lectureName addObject:_lecture];
    NSLog(@"%@",_lectureName);
    delegate.lectureName=_lectureName;
}

假设您正在从mainTableViewController推送addLectureViewController,还要在mainTableViewController的prepareForSegue中包含以下代码(或者您在其中推送addLectureViewController的任何方法)。 : -

addLectureViewController *vc=[[addLectureViewController alloc] init];
vc.delegate=self;
// Push vc

上面的代码实际上创建了一个名为id<mainTableViewController>的弱属性,称为委托(为了防止引用循环,为弱)。 这样,addLectureViewController就可以更新mainTableViewController的属性。

要点:

  1. 为什么mainTableViewController.m的类名为anaTableViewController 它应该是mainTableViewController (几乎总是,直到你获得更高级)。

  2. mainTableViewControlleraddLectureViewController应以大写字母开头。

  3. mainTableViewController访问lecture的唯一方法是通过addLectureViewController对象,例如

     addLectureViewController *alvc = // *something* here NSArray *localLecture = [alvc lecture]; 


    弄清楚如何访问addLectureViewController ,您将解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM