Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

happymonday

macrumors newbie
Original poster
May 21, 2012
3
0
I'm calling method doSomething in my View Controller. The method is declared in my super and sub classes (these classes are for my 3 entities - 1 is parent of other 2). The super class is called SuperClass (not really but for sake of question). I've created an instance for the class called superClass.

[superClass doSomething];

The method returns a string, this string returned is supposed to be text that is entered by the user into a UITextField declared in the ViewController. I cannot get this to work. I had it working fine when everything was contained in the VC but now I'm having to use the entity classes where if no value is returned by the superclass, it looks in the subclass. The attribute is name that is being returned. I need to enter value in headingText (UITextField), have doSomething (method) return that value stored in superClass.name and then cell.displayText.text = superClass.name will display that value. Any and all help is super appreciated! Thanks!

SuperClass.m
Code:
#import "SuperClass.h"

@implementation SuperClass

@dynamic name;

-(NSString *)doSomething
{

    return self.name;
}

@end

SubClassA.m

Code:
#import "SubClassA.h"  //SubClassA.h imports SuperClass.h

@implementation SubClassA

@dynamic body;
@dynamic heading;


-(NSString *)doSomething
{
   [super doSomething];

   return self.name;
}

@end

ViewController.m
Code:
- (IBAction)donePressed:(id)sender {


    AppDelegate* appDelegate = ( AppDelegate* ) [ [UIApplication sharedApplication]      delegate];

    NSManagedObjectContext *context = [appDelegate managedObjectContext];

    SuperClass *superClass = [NSEntityDescription
                        insertNewObjectForEntityForName:@"SuperClass"
                        inManagedObjectContext:context];


   

   superClass.name = headingText.text; //headingText is UITextField

  
   NSString *fromDoSomething = [superClass doSomething];
        
  // I'm missing something here!

    [superClass doSomething];

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%s",__FUNCTION__);

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.textLabel.font = [UIFont systemFontOfSize:19.0];
    }

    NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
    SuperClass *superClass = (SuperClass *)object;
    superClass.name = superClass.doSomething;


    cell.textLabel.text = superClass.name;

    return cell;

}
 
Last edited:

happymonday

macrumors newbie
Original poster
May 21, 2012
3
0
If a method call returns a value, why are you not assigning it to a variable?

I do actually, I have
NSString *fromDoSomething = [superClass doSomething];

I added it after posting but my question is in reference to how to take the value from the UITextField called headingText and get that value to run through the method doSomething within the entity classes.

Can you tell a method in an outside class to run and tell it what values from the VC to pass through it?
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
I do actually, I have
NSString *fromDoSomething = [superClass doSomething];

I added it after posting...

If you have updated code, please post it. It makes it hard for us to help if you don't post the actual code you are running.

As to your question, I would say you should pass the value as a parameter.
 

happymonday

macrumors newbie
Original poster
May 21, 2012
3
0
I actually did edit my code at the same time I wrote my last reply. Thanks for the tip on passing the parameter. However, I ended up taking out NSString *fromDoSomething = [superClass do Something];

Code:
- (IBAction)donePressed:(NSString*) name {
    
    AppDelegate* appDelegate = ( AppDelegate* ) [ [UIApplication sharedApplication]      delegate];
    
    NSManagedObjectContext *context = [appDelegate managedObjectContext];
    
    SuperClass *superClass = [NSEntityDescription
                            insertNewObjectForEntityForName:@"SuperClass"
                            inManagedObjectContext:context];
        
    superClass.name = headingText.text;   
        
    [superClass doSomething];
    
    NSError *error;
    BOOL success = [self.managedObjectContext save:&error];
    if (!success)
    {
        NSLog(@"%@", [error localizedDescription]);
    }
    
  
    
    [self fetchResults];
    
    [self.tableView reloadData];
  
    
}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.