In this application we will see how to  display your app’s preferences or settings in the “Settings” system application.  A settings bundle, included in your application’s bundle directory, contains the information needed by the Settings application to display your preferences and make it possible for the user to modify them.

Step 1: Create a Window base application using template. Give the name of the application “ApplicationSetting”.

Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.

Step 3: Now we’ll add table view controller class to the project. Choose New file -> Select cocoa touch classes group and then select UITableViewController subclass. Give the name “ApplicationSettingViewController”.

Step 4: In our application we need to add Settings.bundle , so select ApplicationSetting -> Add -> New File -> Resources -> Setting Bundle , and give the name settings. Then your will see there are two things are created , 1. Root.plist, 2. Root.strings.

Step 5: In the AppDelegate.h file, we will impliment the NavigationController in the file. Make the following changes in the file.

  enum textColors
 {
   blue = 1,
   red,
   green
 };

 enum backgroundColors
 {
    blackBackColor = 1,
    whiteBackColor,
    blueBackColor,
    patternBackColor
};
@interface ApplicationSettingAppDelegate : NSObject  {
    UIWindow *window;
    UINavigationController   *navigationController;

   NSString   *firstName;
   NSString   *lastName;
   NSInteger  backgroundColor;
   NSInteger  textColor;
}
  @property (nonatomic, retain) IBOutlet UIWindow *window;
  @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

  @property (nonatomic, retain) NSString *firstName;
  @property (nonatomic, retain) NSString *lastName;
  @property (nonatomic, assign) NSInteger backgroundColor;
  @property (nonatomic, assign) NSInteger textColor;

Step 6: Make the following changes in the AppDelegate.m file.

 - (void)applicationDidFinishLaunching:(UIApplication *)application
 {
   [window addSubview:[navigationController view]];

   NSString *testValue = [[NSUserDefaults standardUserDefaults]    stringForKey:kFirstNameKey];
 if (testValue == nil)
  {
    NSString *pathStr = [[NSBundle mainBundle] bundlePath];
    NSString *settingsBundlePath = [pathStr    stringByAppendingPathComponent:@"Settings.bundle"];
   NSString *finalPath = [settingsBundlePath     stringByAppendingPathComponent:@"Root.plist"];

   NSDictionary *settingsDict = [NSDictionary   dictionaryWithContentsOfFile:finalPath];
   NSArray *prefSpecifierArray = [settingsDict objectForKey:@"PreferenceSpecifiers"];

  NSString *firstNameDefault;
  NSString *lastNameDefault;
  NSNumber *nameColorDefault;
  NSNumber *backgroundColorDefault;

  NSDictionary *prefItem;
 for (prefItem in prefSpecifierArray)
 {
   NSString *keyValueStr = [prefItem objectForKey:@"Key"];
   id defaultValue = [prefItem objectForKey:@"DefaultValue"];

 if ([keyValueStr isEqualToString:kFirstNameKey])
  {
    firstNameDefault = defaultValue;
 }
 else if ([keyValueStr isEqualToString:kLastNameKey])
 {
    lastNameDefault = defaultValue;
 }
 else if ([keyValueStr isEqualToString:kNameColorKey])
 {
    nameColorDefault = defaultValue;
 }
  else if ([keyValueStr isEqualToString:kBackgroundColorKey])
{
   backgroundColorDefault = defaultValue;
 }
}

  NSDictionary *appDefaults = [NSDictionary dictionaryWithObjectsAndKeys:
  firstNameDefault, kFirstNameKey,
  lastNameDefault, kLastNameKey,
  nameColorDefault, kNameColorKey,
  backgroundColorDefault, kBackgroundColorKey,
nil];
  [[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
  [[NSUserDefaults standardUserDefaults] synchronize];
}

  firstName = [[NSUserDefaults standardUserDefaults]   stringForKey:kFirstNameKey];
  lastName = [[NSUserDefaults standardUserDefaults]   stringForKey:kLastNameKey];
  textColor = [[NSUserDefaults standardUserDefaults]   integerForKey:kNameColorKey];
  backgroundColor = [[NSUserDefaults standardUserDefaults]   integerForKey:kBackgroundColorKey];
}

Step 7: In the settings.bundle , we’ll see .plist where all the informations are already set what we want to display.

Step 8: In the ApplicationSettingViewController.h file make the following changes.

 @interface ApplicationSettingViewController : UITableViewController {

  UIView *instructionsView;
  UIBarButtonItem *doneButton;
  UIBarButtonItem *flipButton;
}

@property (nonatomic, retain) IBOutlet UIView *instructionsView;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *doneButton;
@property (nonatomic, retain) UIBarButtonItem *flipButton;

- (void)flipAction:(id)sender;

Step 9: Double click the MainWindow.xib file , and open it to the Interface Builder. Drag Navigation Controller from the library and place it to the main window. Open the Navigation controller, drag table view from the library and place it to the navigation window. Now select view controller and bring up Identity Inspector change the class name into the ApplicationSettingViewController. Drag view from the library and place it to the main window change the name into intructionsView. Now drag Bar Button Item from the library and place it on the mainwindow. Connect the Application Setting App Delegate to the Navigation Controller.  Now save the MainWindow.xib file and go back to the Xcode.

Step 10: Now make the following changes in the ApplicationSettingViewController.m file.

 - (void)viewDidLoad
 {
   [[NSNotificationCenter defaultCenter] addObserver:self    selector:@selector(updateSettings:)
  name:UIApplicationDidFinishLaunchingNotification object:nil];

  UIButton* infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
  [infoButton addTarget:self action:@selector(flipAction:)   forControlEvents:UIControlEventTouchUpInside];
   flipButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
  self.navigationItem.rightBarButtonItem = flipButton;

}

 - (void)viewDidUnload
 {
   self.instructionsView = nil;
   self.doneButton = nil;
   self.flipButton = nil;
}

 - (void)updateSettings:(NSNotification *)notif
 {
   ApplicationSettingAppDelegate *appDelegate = (ApplicationSettingAppDelegate *)[[UIApplication sharedApplication] delegate];
   switch ([appDelegate backgroundColor])
 {
   case blackBackColor:
   self.tableView.backgroundColor = [UIColor blackColor];
   break;

  case whiteBackColor:
  self.tableView.backgroundColor = [UIColor whiteColor];
  break;

  case blueBackColor:
  self.tableView.backgroundColor = [UIColor blueColor];
  break;

   case patternBackColor:
   self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
   break;
 }
}

#pragma mark – Flip screen
 - (void)flipAction:(id)sender
 {
   [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:animationIDfinished:finished:context:)];
   [UIView beginAnimations:nil context:nil];
   [UIView setAnimationDuration:0.75];

   [UIView setAnimationTransition:([self.tableView superview] ?
    UIViewAnimationTransitionFlipFromLeft :    UIViewAnimationTransitionFlipFromRight)
    forView:self.tableView cache:YES];

  if ([instructionsView superview])
  [instructionsView removeFromSuperview];
 else
  [self.tableView addSubview:instructionsView];

  [UIView commitAnimations];

  if ([instructionsView superview] == self.tableView)
  self.navigationItem.rightBarButtonItem = doneButton;
else
  self.navigationItem.rightBarButtonItem = flipButton;
}

#pragma mark – UITableViewDataSource

 - (NSInteger)tableView:(UITableView *)tableView  numberOfRowsInSection:(NSInteger)section
{
 return 1;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  NSString *kCellIdentifier = @"MyIdentifier";

  UITableViewCell *cell = [tableView    dequeueReusableCellWithIdentifier:kCellIdentifier];

 if (cell == nil)
 {
   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:kCellIdentifier] autorelease];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
}

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

  NSString *firstNameStr = appDelegate.firstName;
  NSString *lastNameStr = appDelegate.lastName;
  cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", firstNameStr,    lastNameStr];

 switch ([appDelegate textColor])
{
  case blue:
  cell.textLabel.textColor = [UIColor blueColor];
  break;

  case green:
  cell.textLabel.textColor = [UIColor greenColor];
  break;

  case red:
  cell.textLabel.textColor = [UIColor redColor];
  break;
}

  return cell;
}

Step 11: Now compile and run the application in the Simulator.

Did you enjoy this article?
Share
the
Love
Get Free Updates