Showing posts with label iPad. Show all posts
Showing posts with label iPad. Show all posts

Tuesday, February 10, 2015

Using blocks in objective-c iOS7 and iOS8

blocks are a godsend and still people are reserved in using it. Perhaps it is because they don't know how to set it up. Here is a quick example on how to set it up.

In the .h file of whatever you are creating, make a new typedef for the block;

typedef void(^myCompletion)(BOOL complete);

This line shows that this is a "myCompletion" block and has one value going into it which a bool called complete. 

Next in the .h file create the method you want with a completion block;

-(void)doMyStuff withCompletion:(myCompletion)compBlock;

Now all you have to do is create it in your .m file;

-(void)doMyStuff withCompletion:(myCompletion)compBlock{
     //do the stuff you want to do 
     if (compBlock != nil) {
         compBlock(YES);
     }
}

This will do the method and then check if the compBlock is nil (protection) and if it is not nil, perform that block of functions as well. 

Technically you can do the block anywhere in the code, I am just using completion as an example since most of the time, that is where it is used. 

Now you actually want to use this method? Simple;

[myMethod doMyStuff withCompletion:^(BOOL complete) {
     if (complete){

          self.navController.topViewController.navigationItem.titleView = logoView; 
     }
}];

and there you have it. 

if you want another version, you can look here.
Stanford also has a nice video about using existing blocks here.


Thursday, July 11, 2013

How to create a subview that will be a popup on large devices and full screen on small ones in iOS 6 with ARC

Universal apps are becoming more and more necessary today. They are becoming more complicated as well. At least they don't hundreds of different size screens to deal with like android.

First you should have a subview that is the size of a standard smaller screen (itouch, iphone). We will call this "SearchViewController".  You might want to use a delegate to interact with the views but we won't worry about that in this exercise.

in the SearchViewController.m;

-(void) closeView{
    //all keyboard items should have "resignFirstResponder" attached to it here just in case
    [self.view removeFromSuperview];
}

in the MainViewController.h;

#import "SearchViewController.h"

@interface MainViewController <UIPopoverControllerDelegate>{
    SearchViewController *searcher;
}

in the MainViewController.m;

-(void)OpenSearch{
    static bool isSearchOpen = FALSE;
    //open up the subview for the search function. Open up as a mini view for the ipad and a full window view for the iphone.
    if (isSearchOpen == FALSE){
        isSearchOpen = TRUE;
        if (searcher == nil){
            searcher = [[SearchViewController alloc]init];
            searcher.delegate = self;
            if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
                CGRect frame = searcher.view.frame;
                searcher.view.frame = CGRectMake(0, 0, frame.size.width, frame.size.height-88);
            }
        }
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
            //iphone
            [self addChildViewController:searcher];
            [self.view addSubview:searcher.view];
            [searcher didMoveToParentViewController:self];
        }else{
            //ipad
            popSearch = [[UIPopoverController alloc] initWithContentViewController:searcher];
            popSearch.popoverContentSize = searcher.view.frame.size;
            popSearch.delegate = self;
            [popSearch presentPopoverFromRect:CGRectMake(self.view.frame.size.width - 5,0, 10, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
        }
    }else{
        isSearchOpen = FALSE;
        [self closeSearch];
    }    

}

-(void)closeSearch{
    //cancels the search and closes the search view
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
        [searcher.view removeFromSuperview];
    }else{
        [popSearch dismissPopoverAnimated:YES];
    }
    isSearchOpen = FALSE;
}

Done :)

Wednesday, June 12, 2013

Different ways to differentiate between devices in iOS

There are many ways to figure out how to tell which device you are using, some are specific, some are general. They all serve their purpose. Here are some examples;

If you have a universal app and want to use a different nib for the ipad or iphone;
    NSString *nibName = [NSStringFromClass([self class]) stringByAppendingString:([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)? @"-iPhone" : @"-iPad"];
    self = [super initWithNibName:nibName bundle:nil];
* note be sure and name you nibs correctly.

If you have some specific code the you want to perform on either the iphone or ipad;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
    //iphone code
}else{
    //ipad code
}

If you want a quick definition change depending on if it is an ipad or iphone;
    int total = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)? iPhone_Total :iPad_Total;

Thursday, May 2, 2013

iOS Creating Custom Delegates in iOS6 with ARC

So you want two Objects to talk to each other more than a little bit? Here is what to do by example.  Lets say we have our MainViewController and a new SearchViewController that you want a delegate attached to it. This is all you need to do.

In SearchViewController.h
@protocol SearchViewDelegate <NSObject>
@optional
@required
-(void)didAskToSearch:(NSString*)string;
@end

@interface SearchViewController : UIViewController {
}
@property (nonatomic, assign) id <SearchViewDelegate> delegate;
@end

optional has non-required methods and required of course has required methods. 

In SearchViewController.m;
    [_delegate didAskToSearch: [self searchRequestDetails]];

this is put anywhere in the code when you want it to send the information back to the other object. 

In the MainViewController.h;
@interface MainViewController : UIViewController <SearchViewDelegate> {
}

In MainViewController.m;
SearchViewController *searcher = [[SearchViewController alloc]init];
searcher.delegate = self;

#pragma mark - SearchViewControllerDelegates
-(void)didAskToSearch:(NSString*)string{
     //do whatever you want with the string results
}

When creating the object, make sure the tell the delegate you have those methods. Then you just have to create the methods as per it is required. 

Helpful link
http://stackoverflow.com/questions/12020539/how-to-make-custom-delegate-in-ios-app

Thursday, March 7, 2013

Programmatically Adding UINavigationController to existing app in iOS 6 with ARC

Whenever you build an app, you think you know how it should be structured. How are the views going to be layed out for the user to see. However, sometimes designs have to change (Mostly because the client keeps a moving target). How can you add that very helpful Navigation Controller into an existing app quickly and easily...

First, don't panic. It is actually quite easy. the guys at apple feel your pain and tried to make this as confortable as possible. 

In the AppDelegate.m;

    UINavigationController *navCtrlr = [[UINavigationController alloc]initWithRootViewController:self.viewController];
    [self.window setRootViewController:navCtrlr];


Where "self.viewController" is your existing viewController that has your first view. 
Done. 
Thats it. 
Ta Da!

now all you need to do is go through your code and turn the "addSubview" to this;

     [self.navigationController pushViewController:_detailViewController animated:YES];

Where "_detailViewController" is your next view in the stack you want to view. 

As you know, navigationController has a auto back button, but if you want to do it manually;

 [self.navigationController popViewControllerAnimated:YES];

or
 [self.navigationController popToRootViewControllerAnimated:YES];
if you want to go all the way back to the beginning. 

Other quick cosmetic items you can do is;

-(void) viewWillAppear:(BOOL)animated{
    self.title = @"My Title";

    self.navigationController.navigationBarHidden = NO;
}

Which will let you change the title on the NavBar. And make sure it is not hidden.

- (void)viewDidLoad{
    [super viewDidLoad];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleBlack];
}

Which will let you change the colour of the NavBar. 

There is a lot more that can be done, but I hope that gets you off the ground and in the right direction. 

Here is an add on that looks interesting;
iOS Open Source: Drop-Down Navigation Menu