saving textfield text as a .txt file from NSdictionary key

Discussion in 'iOS Programming' started by gbenna, Mar 3, 2012.

  1. macrumors member

    Joined:
    Jul 27, 2011
    #1
    I am trying to save the text entered into a UITextField in a DetailView as a .txt file into the Documents Directory. I get the name of the .txt file from an NSMutableDictionary in a mainView which is a TableView.

    Here is my code in the MainView;

    Code:
      - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
            const NSDictionary *const row = [self rowForIndexPath:indexPath];
            
            NSString *wantedClassName = [row objectForKey:@"controller"];
            
            UIViewController *const vc = [[NSClassFromString (wantedClassName) alloc] init];
            NSLog(@"controller is -%@-", wantedClassName);
            
            
            if ([vc isKindOfClass:[DetailViewController class]])
            {
                DetailViewController *DetailView = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
                **//This is where I think my problem is**
                NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                  @"image.jpg",@"photokey",
                                                  @"name.txt", @"identity", nil];
                
                DetailView.myDictionary = myDictionary;       
            }
            [self.navigationController pushViewController:vc animated:YES]; 
            }
    In my DetailViewController I have this code to save the text;

    Code:
     -(IBAction)saveText:(id)sender
        
        {
                [self.name resignFirstResponder];
            NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
            NSString *documentsDirectory = [documentPaths objectAtIndex:0];
            NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:[(NSMutableDictionary *)familyDictionary objectForKey:@"identity"]];
            NSString *savedString = name.text;
            
            NSLog(@"The Save file is:%@", savedString);
            
            [savedString writeToFile:documentTXTPath atomically:YES
                            encoding:NSUTF8StringEncoding error:NULL];
        }

    I can input the text into the textfield and hit the button. The log shows the text being entered but I think I am not getting the 'key' from the dictionary the text can't be written to a txt file. It doesn't show up in the Document's Directory. I know that I could save this to NSUserDefault but I have my reasons why I don't want to. Can someone help me figure out how to access the Dictionary key?

    Thanks
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    What is the difference between DetailViewController's myDictionary property and familyDictionary?
     
  3. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    writetToFile returns a BOOL and also an NSError* when it fails. You should inspect those.

    Why does your didSelectRowAtIndexPath: method create two view controllers and abandon one of them?
     
  4. thread starter macrumors member

    Joined:
    Jul 27, 2011
    #4
    saving text changes to NSMutableDictionary from UITextView

    there should be and now isn't any difference between familyDictionary and myDictionary. It was a typo.

    I see what you mean about the view controller.

    I want to use the wanted view controller because then I can choose from different view controllers depending on the cell selected from a plist. So I have changed the code to this.


    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        const NSDictionary *const row = [self rowForIndexPath:indexPath];
        
        NSString *wantedClassName = [row objectForKey:@"controller"];
        
        UIViewController *const vc = [[NSClassFromString (wantedClassName) alloc] init];
        NSLog(@"controller is -%@-", wantedClassName);
        
        
        
        if ([vc isKindOfClass:[detailViewController class]])
        {
            detailViewController*lcVC =(detailViewController*)vc;
      
         NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                                 
                                                 @"image.jpg",@"photokey",
                                                 @"name.txt", @"identity",nil];
          lcVC.myDictionary = myDictionary;   
            
            
            NSLog(@"numberkey");
            
        }
        [self.navigationController pushViewController:vc animated:YES];
        
    }
    I don't get any errors or warnings with this and logs show the text I am inputting in the simulator. But I don't get anything from the NSLog(@"number key"); above.

    thanks for the help.
     
  5. thread starter macrumors member

    Joined:
    Jul 27, 2011
    #5
    saving text changes to NSMutableDictionary from UITextView

    Sorry stupid me I had put the NSMutableDictionary into the wrong ViewController tableview. DOH!!! that part works fine now but I'm sure I have other legit questions.
     

Share This Page