-
February 20, 2012 – 11:05 pm
It’s easy enough to navigate from a manually added button to a specific View in the MainStoryboard. But it’s a little less straightforward when such an action is required off dynamically generated controls. The past few days this is what I’ve been crunching on and here is the solution I finally got to.
First off I created a button:
1 2 3 4 5 6 7
| [self makeThumbnail:nil
atPosX:0
atPosY:0
withSizeW:200
withSizeH:50
withTag:0
inView:self]; |
Inside the method that generates the button I also have a line that adds a specific action to it.
1
| [myButton addTarget:self action:@selector(pushToNextView:) forControlEvents:UIControlEventTouchUpInside]; |
And the called method:
1 2 3
| -(void)pushToNextView:(UIButton*)sender {
[self performSegueWithIdentifier:@"loadNextViewSegue" sender:sender];
} |
Now pay attention to the “loadNextViewSegue” part. Basically I found that 2 views inside the storyboard can be connected with a segue directly. Ctrl+click the first one and drag to the next one. These kinds of segues do require an identifier. The identifier can be easily set in the Attributes Inspector with the particular segue selected. I called mine loadNextViewSegue.
Going back to the previous segue isn’t as easy as I thought it would be. About this in one of the future posts.
-
February 18, 2012 – 12:05 am
It wasn’t too long after starting to work with Classes in Objective C that I started to wonder how could I dynamically instantiate an UIButton, UIView ( or any other UI element for that matter ) on the Main view. I am sure this is pretty trivial for an experienced Objective C developer but it’s not that easy to figure out as a n00b.
In first place if you ever read anything about Objective C you have probably noticed that some methods are declared with a – and others are declared with a +:
1 2
| -(void) instanceMethod;
+(void) classMethod; |
You’d call these in different ways:
1 2 3 4
| MyClass *newInst = [[MyClass alloc] init];
[newInst instanceMethod];
[MyClass classMethod]; |
Let’s hop to our code. In this example I shall make a Class method that created a UIButton. The UIButton properties will be set through parameters.
CreateButton.h
1 2 3 4 5 6 7 8
| +(void) makeButton : (UIButton *) myButton
Xcoord : (double) X
Ycoord : (double) Y
withWidth : (double) WIDTH
withHeight : (double) HEIGHT
withTag : (int) btTag
withTitle : (NSString *) btTitle
inView : (UIView *) theView; |
CreateButton.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| +(void) makeButton : (UIButton *) myButton
Xcoord : (double) X
Ycoord : (double) Y
withWidth : (double) WIDTH
withHeight : (double) HEIGHT
withTag : (int) btTag
withTitle : (NSString *) btTitle
inView : (UIView *) theView {
myButton = [UIButton buttonWithType :UIButtonTypeRoundedRect ];
myButton.frame = CGRectMake (X, Y, WIDTH, HEIGHT );
[myButton setTitle :btTitle forState :UIControlStateNormal ];
[myButton setTag :btTag ];
[myButton setBackgroundColor :btColor ];
[theView addSubview :myButton ];
} |
At this point we have a method that creates a UIButton object and places it at specified coords on the stage on the specified view. We had to obviously include the CreateButton.h in our ViewController.h in order for this method to be visible.
ViewController.m
1 2 3 4 5 6 7 8
| [CreateButton makeButton:randomButton
Xcoord:0
Ycoord:0
withWidth:200
withHeight:50
withTag:1
withTitle:@"Some title"
inView:self.view]; |
At this point everything seems ready but the compiler should throw you an error “Use of undeclared identifier randomButton“. This really puzzled me as I didn’t exactly understand what the actual problem was. But I then understood and it seemed very logical – randomButton has to be declared in ViewController.h like this:
1
| @property(nonatomic, weak) UIButton *randomButton; |
The result should look something like this:

Bare in mind – this code has been developed in XCode 4.2 with use of .storyboard and ARC ( this is why you won’t see any releases )
-
February 14, 2012 – 9:19 pm
Some things should be kept as they are even if it might seem cliché. So here it is – the Hello World!
And if I am to remember the easiest “Hello World!” I ever made it must be the iPhone one. It was a simple label in a .nib.
I am passionate about design and programming. At this point I am digging my way through iPhone development and this place, the blog, will be were I’ll drop some thoughts about what I shall encounter along the way. Of course my design work will make no exception.