1

i am a .net programmer and last week i started to read about objective-c. Class related stuff are kinda clear and today i learnt about protocols and delegates, i can't say it is 100% clear but i got it, it looks a lot with delegates and events from c#.

This is a simple example i created following a tutorial. It is all about 2 screens, the first one(a label and a button) launches the second one(a textbox and a button) which sends back a string. I think of it as a classic example of using events, no matter the programming language.

#import <UIKit/UIKit.h>
#import "ValueViewController.h"

@interface ViewController : UIViewController<ValueViewControllerDelegate>

- (IBAction)btnGetValue:(id)sender;

@property (weak, nonatomic) IBOutlet UILabel *lblCurrentValue;

@end

#import "ViewController.h"
#import "ValueViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnGetValue:(id)sender {
    ValueViewController *valueVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ValueViewController"];
    valueVC.delegate=self;
    [self presentViewController:valueVC animated:FALSE completion:nil];
}

-(void) sendValue:(ValueViewController *)controller didFihishWithValue:(NSString *)value
{
    self.lblCurrentValue.text=value;
}

@end


#import <UIKit/UIKit.h>

@class ValueViewController;

@protocol ValueViewControllerDelegate<NSObject>

-(void) sendValue:(ValueViewController*) controller didFihishWithValue:(NSString*) value;

@end

@interface ValueViewController : UIViewController<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *txtValue;

- (IBAction)btnSetValue:(id)sender;

@property (weak, nonatomic) id<ValueViewControllerDelegate> delegate;

@end

#import "ValueViewController.h"

@interface ValueViewController ()


@end

@implementation ValueViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.txtValue.delegate=self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}

- (IBAction)btnSetValue:(id)sender
{
    [self.delegate sendValue:self didFihishWithValue:self.txtValue.text];

    [self dismissViewControllerAnimated:FALSE completion:nil];
}
@end

My question is the following: Considering a, let's say, 30 screens application, which allows sending and receiving messages, adding friends , etc Is it a good approach to group those 4-5 message view controller into a storyboard, those friends related view controllers into another storyboard and just make the connection like i did in that simple example, programmatically?

I saw that connections can be done in the designer without writing code, but sometimes i think you have to write code to send some arguments which means mixing the two(graphically and programmatically). I just feel more comfortable, doing it programatically, maybe because this is how i do it in c#.

I am looking forward to you tips regarding organizing and making connections between screens.

PS: Sorry for writing such a long story(board) in here, i promise to make it shorter in my following posts.

Thanks.

David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205

1 Answers1

0

Making two storyboards that communicate with each other would go against the intended flow, because storyboards were not intended for grouping parts of an application. Although an app may definitely have multiple storyboards, the intention behind allowing multiple storyboards was letting you support different screen paradigms (i.e. iPhone vs. iPad) or different localizations, not grouping related screens together.

Note, however, that storyboards are relatively new. You can define your views in NIB files, and use them instead. An unfortunate consequence of this choice is that you would need to make all your connections programmatically, but on the other hand you would be able to group your views inside you Xcode project using file groups or folders.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • From what i've understood NIB files are obsolete and storyboards are the way to go. – user2611691 Jul 23 '13 at 18:39
  • 1
    @user2611691 No, NIBs are not obsolete. They are not even deprecated, and I don't see how or why would they be desupported ([link](http://stackoverflow.com/a/8495940/335858)). – Sergey Kalinichenko Jul 23 '13 at 18:44
  • That makes it clear that NIBs are not deprecated, regarding those 30-40 screen, can they all be on a single storyboard, is it too much? – user2611691 Jul 23 '13 at 19:40
  • @user2611691 That can certainly be done, too: [here is a post with an image of a storyboard with 24 screens](http://michaelmangold.com/?p=19). I'm sure you can double that while keeping your layout reasonably neat, as long as your screen-to-screen flow makes sense. – Sergey Kalinichenko Jul 23 '13 at 19:47
  • Thanks a lot for clarification. I hope we'll meet again because i will definitely post more question in here. – user2611691 Jul 23 '13 at 19:57