Is it possible to build an iphone view WITHOUT using Interface Builder? - iphone

So Interface Builder does things to save time but what if I want to do without it ? In C# all code generated is clear and understandable, in interface builder this is hidden so I'd like at least during learning phase do things from scratch: any sample code to do so ?
Seems Apple makes things very hard so as you cannot easily use alternative to Xcode :)
http://cocoawithlove.com/2009/02/interprocess-communication-snooping.html

Yes, you can build a view without IB. Just allocate and initialize a view or a control and set its properties, e.g.:
UITextField *tf= [[UITextField alloc] initWithFrame: CGRectMake(24.5, 65, 270, 30)];
tf.delegate = self;
tf.textAlignment = UITextAlignmentCenter;
tf.autocorrectionType = UITextAutocorrectionTypeNo;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
[self.view addSubview: tf];
But IB does not hide any generated code from you because it doesn't generate any code at all. Instead, it creates object instances and serializes them into a XIB or NIB file.

Interface Builder doesn't work in the same way as Visual Studio. It doesn't generate code, instead it generates "raw" object instances. In this sense it's not possible to see the code because there is no code generated.
One option nib2objc to convert your interfaces to code, though I'm not sure how readable that would be. As noted above, this is not the code that would be executed by your iPhone.

This blog post shows you how to get started with a nibless iPhone project.
And you might want to look at this SO question to learn about creating views (e.g. a UIButton in this case) programmatically.

Interface builder doesn't generate code to build a UI object. It creates a compressed xib data object which the iOS runtime "uncompresses" (does some housecleaning) into your pre-built object.
The paradigm is to actually think of you objects as objects in their own right, and not just the results of code execution. e.g. what you would end up with if you could "Make it so." without writing any code.

Related

creating an iPhone app without using the Interface Builder

I'm looking into creating a simple iPhone app without using the interface builder, i.e. without creating the XIB files.
I've succeed so far in showing the main window and changing the background color, but i'm looking into adding UITextfield, UILabel, and a button. and then connect them to methods that i've created before.
is there any good tutorial or a reference that I can use?
Thank you very much
Read Apple's documentation. For example, to create a UILabel programmatically, read the UILabel Class Reference. First instantiate it
UILabel *label = [[UILabel alloc] init];
Then set it up as you like, e.g.
[label setText:#"My label now has text!"];
Then add it to the view
[self.view addSubview:label];
Then move it around, etc. For more help, first google "create uilabel programmatically", then, if you get stuck, post a specific question here.

Encapsulation of logic and interface on iPhone SDK

I assume this is going to be a very basic question, but maybe somebody can help me. I've come to iPhone SDK from a C# .NET background.
I would like to know if there is some mechanism similar to what is called in ASP.NET "UserControl" which encapsulates logic and interface. It would be desirable to launch "events" too.
I'm trying to design something like a common header for the entire application, which shows different types of buttons depending of the view where it is placed.
Normally Objective C will support only single Inheritance.We can achieve multiple inheritance
through Delegates called as protocols(user defined).i think it will help you when you google based on this(user defined protocols in iphone sdk).
First of all thanks for your contribution. I realized that I was looking for something different: The views (yeah, I know there were here since the beginning).
I've realized that when I allocate one of my controllers by default it tries to load the xib with the same name. Therefore, I can design the view on the interface builder and then load it on another view as a subview (that's similar to usercontrol concept).
How to make the view adjust to the desired size?:
Controller *controller = [[Controller alloc] initWithNibName:nil bundle:nil];
controller.view.frame = CGRectMake(100, 100, 200, 200);
[self.view addSubview:controller.view];
The only missing here is how to comunicate the view with the subview, but I supose that can be made with a reference to the parent.
Thanks a lot
(PS: I'm going to check the answer as correct just for close the thread. I don't know if this procedure is the best or has enough quality)

What is the best way to create multi-theme application

I need to create an application that should have several themes. It means that for each theme we use different set of images. The theme should be changed without full reload of all view controllers (just change the images). The themes could be added via In app purchases so they should not be hardcoded.
I also want to use Interface Builder to create my views.
Question: what is the best way to do that?
On MyAppNotificationUIThemeChanged every controller has to retrieve and set images for every UIImageView in it's main view. I can think of 2 approaches for doing this:
1) Brute force. Something like this:
self.imageView1.image = [self currentThemeImage:#"someController_image1.png"];
self.imageView2.image = [self currentThemeImage:#"someController_image2.png"];
2) Automated approach.
NSArray *imageViews = [self fetchAllImageViews];
foreach (UIImageView *iv in imageViews) {
iv.image = [self currentThemeImageForTag:iv.tag];
}
where currentThemeImageForTag: is something like this:
- (UIImage*)currentUIThemeImageForTag:(NSUInteger)imageTag {
return [self currentUIThemeImage:[NSString stringWithFormat:#"%#_%u.png", NSStringFromClass([self class]), imageTag]];
}
I also suggest to use NSBundle to package theme images and other resources.
Update:
I chose NSBundle only because of it's cool methods like pathForResource:ofType: and such and also ability to support localization. You can create bundle with XCode (just look under Mac OS X -> Framework&Library section in new project dialog). Put your images in Resources, remove liks to any frameworks (there is no code anyway), build and that's it. In your app load bundle with [NSBundle bundleWithPath:pathToDownloadedBundle]. Beware though: I don't know if Apple allows to download bundles and use it's content in app. All I can guarantee is that it works on simulator.
I would suggest the approach that I detail in my answer here:
Is there a simple way to set a default font for the whole app?
You could have view controllers in your app ask a ThemeApplicator class to style them when necessary. It would do this by setting the correct background image, that sort of thing. You can still use IB to build your views; just don't set a background in IB (or set a default background). The key is that you programmatically update the background later.
Look at Three20 project, especially at Three20Style.

iOS media picker does not show up

I am currently working on an audio application on iPhone. It is based on apple's SpeakHere sample code with a user-defined input file from iPod library.
Here is the event raised by the button:
- (IBAction) btn_PickSong_Clicked:(id)sender{
[self showMediaPicker];
//code importing tracks from library
}
And in showMediaPicker method:
//Yup the program does reach this method but the picker does not show up
- (void)showMediaPicker {
MPMediaPickerController* mediaPicker = [[[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic] autorelease];
mediaPicker.delegate = self;
[self presentModalViewController:mediaPicker animated:YES];
}
The problems are:
The library import feature works fine in a separate program, but the media picker does not show up anymore when I put the code into SpeakHereController.mm.
Also if I place the showMediaPicker method in another class and call it, it does not work either.
Something I find it might be relevant:
The original code is in an obj-C file (xxx.m), and now it's transferred into an obj-C++ file (xxx.mm).
I have also modified the base class of SpeakHereController.h from NSObject to UIViewController<MPMediaPickerControllerDelegate> otherwise it will throw a warning that the base class does not contain the required delegate. But in the interface builder it is still displayed as an object (Please refer to SpeakHere sample code).
It seems that it's illegal to convert the built-in xxxViewController.m file to obj-C++ file (.mm extension). In this case a lot of errors will show up if I attempt to do so. Is it true? If so, how to include C++ code in a pure obj-C file?
=============
So how can I make the media picker show up in this case? Any insight will be appreciated.
Thank you very much!
Cheers,
Manca
In order for
[self presentModalViewController:mediaPicker animated:YES];
to work, self needs to be a viewcontroller. I'm worried that you have just changed the base class to avoid compiler errors as this suggests you are not actually instantiating 'self' correctly.
So how are you initialising the SpeakHereController? As a view controller, this would normally be via the designated initialiser, which for a UIViewController is of course initWithNibName:bundle:
You may find the documentation for UIViewController helpful.
With regards to the C++ issue. Although you can mix objective-c and c++ in the way you suggest, I would recommend that you encapsulate your c++ code in it's own class rather than sprinkling it around your viewcontroller code. That will make it more maintainable for the future.

General question about iPhone programming

I have a question regarding nibs and how detail views are created in professional apps. I want to make an app that loads different uitextfields for each nib that is selected from a table. These textfields contain some logic that is different from each. I wanted to ask if it's possible to make one nib and change the data from thatto match all these scenarios. I'm unsure how this is done and how nib management can be done by arrays. Thanks and I hope I was clear enough :)
I can only speak myself but I've moved away from nibs. When you get complex layouts that have elements disappearing and reappearing your nib/s become unmanageable. You can do a better job of keeping elements tidy and manageable in code as well as a slightly faster App (not much but its a nice bonus).
EDIT: As a very basic example just remove any details of a nib and if you class is a UIViewController subclass do this:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50,50,150,30);
label.text = #"Hello World";
[self.view addSubview:label];
[label release];
Very basic but hopefully this will get you on your way to developing without nibs. Note: Nibs aren't bad and if its a basic view use them but if you are having complex views you will find nibs becoming unmanageable.
I found this article on Cocoa With Love to be really interesting in regardless to performance of nibs vs code.
IB / Nibs are useful for "getting started" and quick prototyping. However like #Rudiger mentions it's useful only in simple, static layouts. Usually it's only a matter of time before you'll run into limitations and find yourself having to write real code.