iOS Programming Tutorial – Protocol Delegate method of passing data

Passing data using the protocol delegate pattern.

“Objective-C allows you to define protocols, which declare the methods expected to be used for a particular situation.”

“A protocol, by contrast, is used to declare methods and properties that are independent of any specific class.”

“A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.”
Continue reading

iOS Programming Tutorial – Core Data Using A NSFetchedResultsController

Today we’re going to expand on the first Core Data tutorial in this series We’ll be changing the table view to use an NSFetchedController which really makes keeping track of changes in our data and handles the updating of our view for us.

If you want to grab the code from part one which we’ll be starting with go here.

1.) To start off we’re going to strip out all the parts of the code that we won’t need now that we’ll be using the fetched results controller.

Open up PeopleTableViewController.m

Remove the properties for the array’s and the fetch request. Here’s what our private interface portion looks like now.

@interface PeopleTableViewController ()
@property (nonatomic, strong) Person *person;

Next remove the fetchTheData method and all references to it.

Also remove the viewWillAppear method.

Then remove any commented out methods.

I’m also going to remove the didReceiveMemoryWarning method, we won’t need it for this demo.
Continue reading

iOS Programming Tutorial – Local Notifications with iOS 8

Today we’re going to take a look at creating Local Notifications in iOS 8. The process of creating notifications changed with iOS 8. We now need to ask the user for permission to display notifications and register the settings.

This will be a short tutorial, and I won’t waste your time detailing how to set up an app step by step.

1.) Start by creating a single view app named NotifDemo and with a UI that looks something like this.

Description of pic
Continue reading

iOS Programming Tutorial – UIScrollView

Implementing a UIScrollView is a very simple thing to do. It’s really just a matter of adding the scroll view to your existing view and then adding a dew methods to the controller.

This will be one of the shorter and easier tutorials we do. Because our focus in on the scroll view itself, I’m not going to take time to explain what we’re doing when we’re setting up our app.

1.) The first thing we need to do is set up a very simple application to work with. Create a single view application and name it ScrollViewDemo.

We’ll do all of our work in the single view and controller that Xcode create for us. Before we add the scroll view let’s just display an image in the view. Open up ViewController.m add add these properties.

#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIImage *image;
@property (nonatomic, strong) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) NSURL *imageURL;

Continue reading

iOS Programming Tutorial – Core Data Part 1: Passing Around NSManagedObjectContext

I originally started doing iOS development sometime back in 2010, before my 3 year exile into .net. This was a time before Storyboards and as a developer you found yourself doing a lot of work in the didFinishLaunchingWithOptions method. In fact you could set up all your controllers there. You’d create a Tab Bar controller and hook it up with a Navigation Controller and then add all kinds of view controllers to it, all right there in the app delegate. While this added a lot of code that is not necessary these days using Storyboard, it did make it a pretty easy thing to pass the managedObjectContext for your Core Data into each of the controllers.

When I finally came back to iOS, after my time spent in the wilderness of C#, I had to learn all about storyboards and what they did to change the way I had previously developed my apps. Beyond just saving a lot of UI code from being written, one of the first things I discovered was my old methods of using Core Data were a bit stale. Thus my adventure in learning the new methods for Core Data and even more so passing around a managedObjectContext.

Description of pic
Continue reading

iOS Programming Tutorial – Storyboard Tutorial

This tutorial will be an introduction to using Storyboards in Xcode. Storyboards were introduced with the release of Xcode 4.2 and simplified the process of creating UI for apps. Prior to the introduction of Storyboards developers had to create a new nib file for each view and associate it with a view controller managing how all those views related to each other. Storyboards changed all of that and allowed the developer to spend more time focusing on the logic of the app and less time coding for the UI.

If you have used Storyboards you will not find anything new here. But if you are just starting out follow along and see how easy storyboards make our life.

Let’s get started.

1.) Open up Xcode and create a new Tabbed Application project named StoryboardTest.

Description of pic

Continue reading

iPhone app development tutorial – add an intro view to your tab bar app

This tutorial was originally published in April 2011

This tutorial is on adding an intro view to your app. In this instance we’ll be adding it to a tab bar app. This is not the use of a splash view. A splash view is one which is shown each time an app loads and is only shown for a few short second, going away without any user interaction. Our intro view will be one that is shown at a specific time, mostly when the app is loaded for the first time. The view will state visible until the user takes some action to dismiss the view. We will be showing our view modally and using a boolean to determine whether to show the view when the app is loaded. In addition we will be storing the boolean in a plist and loading it’s value to determine whether to show our intro view.

So now that we’ve laid out what we want to do, let’s get started.

Simulator Shot
Continue reading

iPhone App Development – Add a twitter button in your app

This tutorial was originally published in March 2011

This tutorial is going to be about adding a Twitter button to your app. By doing this you allow a user to tweet straight from your app rather than exiting and using another app. This became available in iOS 5, and it really is easy to do. Our app here is going to be very simple, but the steps to do this can be used in any app.

App Screen Shot

1.) Let’s start by creating a single view app.

Create a Single View Appt

Go ahead and name the app TwitterTutorial, we’ll use Automatic Reference Counting but will not be using a Storyboard.

Name the App

2.) Before we do anything else let’s import the twitter framework. If you are unclear about how to do this you can check this tutorial.

Add a framework in Xcode 4 Tutorial

Add the Twitter Framework


3.) Our view is going to be very simple. I’m going to spice it up just a little by changing the background and adding an image, but the main thing is to just add the button.

Our view

3.) Open up the view controller header file and declare an IBAction.

- (IBAction)tweetAway;

That’s all we have to do in the header file.

4.) Now open up the view controller implementation file and import Twitter.h.

#import "ViewController.h"
#import "Twitter/Twitter.h"

5.) Next create implement the IBAction that we declared in the header file

- (IBAction)tweetAway 
    //This creates the tweet view that we'll present modally when a user clicks the button.
    TWTweetComposeViewController *tweetView = [[TWTweetComposeViewController alloc] init];
    //You set a default tweet to show in the view
    [tweetView setInitialText:@"Here's a tweet from your app"];
    //You can tweet an image
    [tweetView addImage:[UIImage imageNamed:@"twitter-logo.png"]];
    //You can also add a link to the tweet
    [tweetView addURL:[NSURL URLWithString:@""]];
    // Specify a block to be called when the user is finished. This block is not guaranteed
    // to be called on any particular thread.
    // All we are doing is dismissing the modal view when the tweet is sent.
    tweetView.completionHandler = ^(TWTweetComposeViewControllerResult result)
        [self dismissModalViewControllerAnimated:YES];
    //present tweet view modally
    [self presentModalViewController:tweetView animated:YES];

6.) Last thing is to hook up the IBAction to our button.

Link IBAction

7.) And finally run the app.

App Screen Shot

Click the Tweet button and the Tweet view will open modally.

App Screen Shot 2

That’s it, here’s the code.

iPhone App Development Tutorial – Customize table with modal view

This tutorial was originally published in March 2012

This is a very simple tutorial I did to answer a specific question I had a little while back. I was doing some cleaning up of my hard drive the other day and came across it. I figured I’d go ahead and upload it now since I never had, just in case someone else has a similar question at some point.

Honestly all this information is available in some of my other tutorials. In fact this is really just a quick combination of these three previous tutorials.

Add a UITableView to an existing view

Pass data from a modal view

Add a UINavigationController programmatically

Because this is kind of a rehash of information I am not going to get into great detail. Here’s what we’re going to do. Start with a View-Based app, add a UITableView and a UINavigationController to it. Add a button to the navigation bar which will take us to a new view that we will create and present modally. Add a UITextField to our new view and save the text added in it to a NSString and then add a button to take us back to the main view. In the main view we will retrieve the text we saved from the text field in the modal view and display it in the UITableView.

1.) Start by creating a new Single-View Application and name it ModalTableTutorial.

Create Single View Application

2.) In the app delegate implementation file we are going to add the navigation controller.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;

3.) Create a new UIViewController subclass named ModalViewController.

Here’s the header file with a UITextField, NSString and IBAction declared.

#import <UIKit/UIKit.h>
@interface ModalViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (strong, nonatomic) IBOutlet NSString *textValue;

Here’s what I’ve added to the implementation file.

#import "ModalViewController.h"
@implementation ModalViewController
@synthesize textField = _textField;
@synthesize textValue = _textValue;
- (IBAction)goBackToView
    self.textValue = self.textField.text;
    [self dismissModalViewControllerAnimated:YES];
- (void) textFieldShouldReturn:(UITextField *)theTextField
    [theTextField resignFirstResponder];

4.) Here’s what the nib file for the new view looks like.

Modal View

Connect Outlets

Connect Outlets

5.) In the ViewController.h file we import the ModalViewController and declare an instance variable for it. Also create an IBOutlet for a UITableView.

#import <UIKit/UIKit.h>
#import "ModalViewController.h"
@interface ViewController : UIViewController
@property (nonatomic, strong) ModalViewController *modalViewController;
@property (nonatomic, weak) IBOutlet UITableView *myTableView;

6.) Switch to ViewController.m and synthesize the variables, add a button to the navigation bar and create a method to push the modal view in to place.

#import "ViewController.h"
@implementation ViewController
@synthesize myTableView = _myTableView;
@synthesize modalViewController = _modalViewController;
#pragma mark - View lifecycle
- (void)viewDidLoad
    [super viewDidLoad];
    UIBarButtonItem *modalViewButton = [[UIBarButtonItem alloc] initWithTitle:@"Modal View" style:UIBarButtonItemStyleBordered target:self action:@selector(goToModalView)];
    self.navigationItem.rightBarButtonItem = modalViewButton;
- (void)goToModalView
    if (self.modalViewController == nil) 
        ModalViewController *temp = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:[NSBundle mainBundle]];
        self.modalViewController = temp;
    [self presentModalViewController:self.modalViewController animated:YES];
- (void)viewWillAppear:(BOOL)animated
    [super viewWillAppear:animated];
    [self.myTableView reloadData];

7.) Next add the table view delegate methods.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return 5;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [cell.textLabel setText:self.modalViewController.textValue];
    return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

8.) Finally in the ViewController nib file add a UITableView connect it all up.

Connect Outlets

That’s about it. Run the app and try it out.

Go to the modal view and add some put something in the textField.

Simulator View 1

Then go back to the main view and it should populate the tableView.

Simulator View 3

Of course a few more things need to be added to really make this useful. For example I’m sure you don’t want to populate every row in the table with the same value, but you can add that bit yourself.

Here’s the code.