0
votes

I'm new to Objective-C and need help!

I'm following the example "PageControl" which can be found on the net. I added a button to the view in the NIB and hooked up an action which the implementation is found below.

Here is my definition for the view controller being displayed in the page control:

//ContactCardViewController.h
@interface ContactCardViewController : UIViewController 
{
    int pageNumber;
    NSMutableString *s;
}


//ContactCardViewController.m implementation

- (id)initWithPageNumber:(int)page {
    if (self = [super initWithNibName:@"ContactCardViewController" bundle:nil]) {
        s = [[NSString alloc] initWithFormat:@"page: %d", page];            
    }
    return self;
}


- (id)initWithString:(NSDictionary *)_s {
    if (self = [super initWithNibName:@"ContactCardViewController" bundle:nil]) {
        NSMutableString *t = [[NSMutableString alloc] initWithString:_s];
        s = t;
    }
    return self;
}


-(IBAction)callOrAddToContacts:(id)sender
{
    jobtitleLabel.text = s;
}


//AppDelegate.m
//in delegate that loads the scroll view with the view for the current page:

- (void)loadScrollViewWithPage:(int)page {
//init the view this way the page: and the correct page number is displayed
    controller = [[ContactCardViewController alloc]  initWithPageNumber:page ];

//init the view this way,  the value of the first person is always displayed
    controller = [[ContactCardViewController alloc]  initWithString:[[self.allNames objectAtIndex:page] objectForKey:@"firstname"]];

}


Please help me to understand why when the view is created with initWithString and then accessed via the button action only value for the first person in the list is always returned. When i init the view using initWithPageNumber s has the correct value Page: X.

2

2 Answers

0
votes

In the InitWithString code, you're passing in a Dictionary, not a string.

- (id)initWithString:(NSDictionary *)_s {
    if (self = [super initWithNibName:@"ContactCardViewController" bundle:nil]) {
        NSMutableString *t = [[NSMutableString alloc] initWithString:_s];
        s = t;
    }
    return self;
}

You may want to change that to NSString, or change the NSMutableString... to an instance of NSDictionary. One or the other.

0
votes

Found out the problem was in the plist file. The code is fine.