Use of Nssavepanel and Nsopenpanel in Mac OS

Source: Internet
Author: User



Ssavepanel



You recently need to save the document locally, because it is not pictures, music, or movies, and you cannot use these permissions, so use Nssavepanel to let the user customize the save path. Here is the code


 1     NSSavePanel*    panel = [NSSavePanel savePanel];
 2     [panel setNameFieldStringValue:@"Untitle.onecodego"];
 3     [panel setMessage:@"Choose the path to save the document"];
 4     [panel setAllowsOtherFileTypes:YES];
 5     [panel setAllowedFileTypes:@[@"onecodego"]];
 6     [panel setExtensionHidden:YES];
 7     [panel setCanCreateDirectories:YES];
 8     [panel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result){
 9         if (result == NSFileHandlingPanelOKButton)
10         {
11             NSString *path = [[panel URL] path];
12             [@"onecodego" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
13         }
14     }];


-1 The 2nd line of code is to set the default file name



-2 The 3 lines of code are set up prompt information



-3 The 5th line of code is to set the allowed file types



-4 the 6th line of code is hidden extension



-5 The 7th line of code is set to create files



-6 第8-14 Line code is run window



-7 The 9th line of code is to determine whether the user clicked the OK button (the default title appears to be Save)



-8 The 11th line of code is to take out the path the user chooses to save



It is important to note that when you set the file name, you need to bring the extension (such as line 2nd in the example), you need to set the allowed file type (such as line 5th in the example), and set the hidden extension (such as the 6th line in the example) and the file you can create (such as the 7th row in the example). After setting these properties, run the sample code:






At this point, modify the document name (as modified to Onecodego), select Save Path to documents, click Save.



Open Finder and you'll find Onecodego.onecodego in documents.



It is important to note that the saved document can only be read and written when this is opened, that is, when exiting the software or restarting the computer to read and write access to the path file.





NSSavePanel*    panel = [NSSavePanel savePanel];
    NSView *viewExt = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 180, 40)];
    
    NSTextField *labExt = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 10, 80, 20)];
    
    
    
    [labExt setBordered:NO];
    
    [labExt setDrawsBackground:NO];
    
    labExt.stringValue = @"Image type: ";
    
    
    
    NSComboBox *cbExt = [[NSComboBox alloc] initWithFrame:NSMakeRect(80, 8, 100, 25)];
    
    [cbExt addItemsWithObjectValues:@[@".bmp", @".jpg", @".png", @".tif"]];
    
    [viewExt addSubview:labExt];
    
    [viewExt addSubview:cbExt];
    
    [panel setAccessoryView:viewExt];
    
    [panel setNameFieldStringValue:@"Untitle.onecodego"];
    [panel setMessage:@"Choose the path to save the document"];
    [panel setAllowsOtherFileTypes:YES];
    [panel setAllowedFileTypes:@[@"onecodego"]];
    [panel setExtensionHidden:YES];
    [panel setCanCreateDirectories:YES];
    [panel beginSheetModalForWindow:_window completionHandler:^(NSInteger result){ if (result == NSFileHandlingPanelOKButton)
        {
            NSString *path = [[panel URL] path];
            [@"onecodego" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }
    }];





Nsopenpanel



Open User-selected files using Nsopenpanel


 1     NSOpenPanel *panel = [NSOpenPanel openPanel];
 2     [panel setDirectory:NSHomeDirectory()]; 
 3     [panel setAllowsMultipleSelection:NO];
 4     [panel setCanChooseDirectories:YES]; 
 5     [panel setCanChooseFiles:YES];
 6     [panel setAllowedFileTypes:@[@"onecodego"]];
 7     [panel setAllowsOtherFileTypes:YES];
 8     if ([panel runModal] == NSOKButton) {
 9         NSString *path = [panel.URLs.firstObject path];
10         //code
11     }


-1 The 2nd line of code is to set the default path



-2 The 3 line code is set not to allow multiple selections



-3 The 4 line code is set to open the folder



-4 The 5th line of code is set to select the file



-5 The 6th line of code is the set of file types that can be opened



-6 The 8 line of code is to determine whether the user selected is OK button



-7 The 9th row to remove the user-selected path



It is important to note that you need to set the allowed file types, such as the file type allowed by the example is Onecodego, so only files with the suffix Onecodego can be opened in Nsopenpanel, such as:



Use of Nssavepanel and Nsopenpanel in Mac OS


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.