Enter Value in TextField, use method from entity classes, display value in tableview

Discussion in 'iOS Programming' started by happymonday, May 21, 2012.

  1. happymonday, May 21, 2012
    Last edited: May 21, 2012

    happymonday macrumors newbie

    Joined:
    May 21, 2012
    #1
    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;
    
    }
    
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    If a method call returns a value, why are you not assigning it to a variable?
     
  3. happymonday thread starter macrumors newbie

    Joined:
    May 21, 2012
    #3
    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?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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.
     
  5. happymonday, May 21, 2012
    Last edited: May 21, 2012

    happymonday thread starter macrumors newbie

    Joined:
    May 21, 2012
    #5
    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];
      
        
    }
    
     

Share This Page