简体   繁体   English

在Objective-C中代表

[英]Delegate in Objective-C

I am new at Objective-C, and I'm trying to understand delegates. 我是Objective-C的新手,我正在努力了解代表们。 I have searched and read a lot, but nothing is really helping me understand. 我经常搜索和阅读,但没有什么能帮助我理解。 I think that best way to understand this might be asking a question with a sample app. 我认为理解这一点的最佳方式可能是使用示例应用程序提出问题。

I'm trying to create a grade calculator application to test my skills. 我正在尝试创建一个成绩计算器应用程序来测试我的技能。 Here are my files: 这是我的文件:

mainTableViewController.h mainTableViewController.h

#import <UIKit/UIKit.h>

@interface mainTableViewController : UITableViewController

@end

mainTableViewController.m 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 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 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

Everything is okay so far. 到目前为止一切都还好。 But when i try to use the _lectureName NSMutableArray at mainTableViewController.m, I can't see the array. 但是当我尝试在mainTableViewController.m中使用_lectureName NSMutableArray时,我看不到数组。

I know the codes for printing the array in tableView. 我知道在tableView中打印数组的代码。 I know they are not at there right now. 我知道他们现在不在那里。 I just don't understand implement delegate codes to my code. 我只是不明白实现委托代码到我的代码。

If You want to display something on the rows of the table, You can take an NSArray and you have to return the count of the array in the delegate method: 如果要在表的行上显示某些内容,可以使用NSArray,并且必须在委托方法中返回数组的计数:

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

Otherwise, table will not display any elements. 否则,表格不会显示任何元素。 And delegate method cellForRowAtIndexPath will only be called if you are returning a particular array count of number count in your numberOfRowsInSection method. 只有在numberOfRowsInSection方法中返回特定数量的数组计数时,才会调用委托方法cellForRowAtIndexPath。

You can take reference from these links to understand delegates: http://www.tutorialspoint.com/ios/ios_delegates.htm 您可以从这些链接中获取参考以了解代表: http//www.tutorialspoint.com/ios/ios_delegates.htm

How do I set up a simple delegate to communicate between two view controllers? 如何设置一个简单的委托来在两个视图控制器之间进行通信?

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

But in the case of tableView, the delegate methods are internally defined and triggered internally. 但是在tableView的情况下,委托方法在内部定义并在内部触发。 We just need to set those delegates to the controller which acts as a listener. 我们只需要将这些委托设置为充当监听器的控制器。

The below code might have syntax errors but they can provide a summary of delegates for this code. 以下代码可能存在语法错误,但它们可以提供此代码的委托摘要。

Make the following changes :- 进行以下更改: -

mainTableViewController.h mainTableViewController.h

#import <UIKit/UIKit.h>

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

Synthesize the property lectureName in mainTableViewController.m 合成mainTableViewController.m中的属性lectureName

Then make the following changes in addLectureViewController.h 然后在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

Synthesize the property delegate in addLectureViewController.m 在addLectureViewController.m中合成属性委托

Then make the following change :- 然后进行以下更改: -

- (IBAction)addButtonPressed:(id)sender {

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

Assuming that you are pushing addLectureViewController from mainTableViewController, also include the following code in prepareForSegue of mainTableViewController (or whatever method in which you are pushing addLectureViewController). 假设您正在从mainTableViewController推送addLectureViewController,还要在mainTableViewController的prepareForSegue中包含以下代码(或者您在其中推送addLectureViewController的任何方法)。 :- : -

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

The above code actually creates a weak property of type id<mainTableViewController> called delegate (weak in order to prevent reference cycles). 上面的代码实际上创建了一个名为id<mainTableViewController>的弱属性,称为委托(为了防止引用循环,为弱)。 This way, addLectureViewController can update mainTableViewController's property. 这样,addLectureViewController就可以更新mainTableViewController的属性。

Points: 要点:

  1. Why is the class in mainTableViewController.m named anaTableViewController . 为什么mainTableViewController.m的类名为anaTableViewController It should be mainTableViewController (almost always, until you get more advanced). 它应该是mainTableViewController (几乎总是,直到你获得更高级)。

  2. mainTableViewController and addLectureViewController should start with capital letters. mainTableViewControlleraddLectureViewController应以大写字母开头。

  3. The only way for mainTableViewController to access lecture is through a addLectureViewController object, eg mainTableViewController访问lecture的唯一方法是通过addLectureViewController对象,例如

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


    Figure out how you have access to an addLectureViewController and you will have solved your problem. 弄清楚如何访问addLectureViewController ,您将解决您的问题。

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

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