0

I am new at Objective-C, and I'm trying to understand delegates. 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

#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

Everything is okay so far. But when i try to use the _lectureName NSMutableArray at mainTableViewController.m, I can't see the array.

I know the codes for printing the array in tableView. I know they are not at there right now. I just don't understand implement delegate codes to my code.

burakkesepara
  • 306
  • 4
  • 14
  • Why did you include the `anaTableViewController` code when it doesn't appear to be used anywhere? – trojanfoe Feb 13 '14 at 11:44
  • but where is delegates? – HRM Feb 13 '14 at 11:48
  • You mean you want to transfer the data of **_lectureName** to **mainTableViewController**?? – iamyogish Feb 13 '14 at 11:53
  • i mistype the anaTableViewController thing. I fixed it right now. I wanted to type mainTableViewController. And yes i mean the transferring data of _lectureName to mainTableViewController yes. Thanks. – burakkesepara Feb 13 '14 at 11:55
  • Could you add screenshots of your app?? or can you specify if u have used any **segue** between the **mainTableViewController** and **addLectureViewController**? – iamyogish Feb 13 '14 at 11:58
  • i didn't use any segue between those two. the parent is mainTableViewController and the child is addLectureViewController. And i just want to call back an array from child to parent. And i thought that this is about delegate right ? – burakkesepara Feb 13 '14 at 12:04
  • Without having a **Segue** how can u establish a relationship between those two views??? And when u add some data using addLectureViewController and you wish this data to be available mainTableViewController you need to have a relationship b/w them. – iamyogish Feb 13 '14 at 12:10
  • @iamyogish - There are a dozen different ways to establish a "relationship" between view controllers, but of course one must employ one of them if communication between the two is expected. – Hot Licks Feb 13 '14 at 12:14
  • Basically, when you create the second VC you should, after creating it, set it's `delegate` property to point to a delegate object that has access to the first VC's data. Quite often this would be a pointer to the first VC itself. Then, in the second VC, one invokes methods of the delegate object (usually defined in a "protocol" to keep them distinct) to access the data. – Hot Licks Feb 13 '14 at 12:19

3 Answers3

2

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:

- (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.

You can take reference from these links to understand delegates: 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

But in the case of tableView, the delegate methods are internally defined and triggered internally. We just need to set those delegates to the controller which acts as a listener.

Community
  • 1
  • 1
  • Thanks but i know that i just forgot to write it down there. Which is i edited now. I just want to understand that the delegate codes. Which codes should i write to delegate my array to other page ? I would be very happy if you can help me. Thanks. – burakkesepara Feb 13 '14 at 11:50
  • I edited my answer. Look at it. The UITableView delegate methods are predefined and are called as soon as you set tableview.delegate = self. This is basically a callback mechanism. – Sudershan shastri Feb 13 '14 at 11:53
0

The below code might have syntax errors but they can provide a summary of delegates for this code.

Make the following changes :-

mainTableViewController.h

#import <UIKit/UIKit.h>

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

Synthesize the property lectureName in mainTableViewController.m

Then make the following changes in 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

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). :-

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). This way, addLectureViewController can update mainTableViewController's property.

Max
  • 4,067
  • 1
  • 18
  • 29
0

Points:

  1. Why is the class in mainTableViewController.m named anaTableViewController. It should be mainTableViewController (almost always, until you get more advanced).

  2. mainTableViewController and addLectureViewController should start with capital letters.

  3. The only way for mainTableViewController to access lecture is through a addLectureViewController object, e.g.

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


    Figure out how you have access to an addLectureViewController and you will have solved your problem.

Clay Bridges
  • 11,602
  • 10
  • 68
  • 118