Controls on UIView does not work after adding uiview to uiwindow - iphone

I am trying to work on a universal app for iOS platform, I created a window based application with core data support.
In my AppDelegate_iPhone I added this line in applicationDidFinishLaunching
[window addSubview:myiPhoneMainView.view]; of course I have created & synthesized the myiPhoneMainView variable.
In my MainWindow_iPhone.xib I added a view controller set its class to MyiPhoneMainView and controlled dragged from app delegate to MyiPhoneMainView.
Then I went on & added controls on my MyiPhoneMainView. Everything works fine MyiPhoneMainView gets loaded but the problem is none of the controls are responding to the user events.
I have checked user interaction is enabled on all of them. Niether uibotton nor uitextfield nothing seems to respond to anything.
Can somebody please point out what am I missing.
Thank you .

It sounds like your connections from your view's controls are not linked to the IBActions in your view controller.
Are you loading the controller via initWithNibNamed: or are you just using init? If using initWithNib, then have you linked up the controls in the view with the IBOutlets?
If yes to those, then make sure you are not creating a new view (in the init method or the viewDidLoad of the controller) that replaces the one created in the xib.

Related

What are the possibilities of getting NULL IBOutlets while loading a view.. IOS

I have been working on IOS application. Before I had single project in which all source code was there application loaded properly in that setup, now I had split that into multiple projects. After that I am facing now a problem... in ViewDidLoad, IBOutlet for buttons all are coming nil values, view also loading black colored. I am not able to guess what was the problem. Any idea about what could cause this...
I have loaded my view like this...
main =[[main_page_controller alloc] init];
if (main != NULL)
[root.navigationController pushViewController:main animated:YES];
I am not sure which part of the code do I need to post here, to make the question more understandable... Please share your suggestions..
Edit: I ran my old project and then tried with my new set up application launching successfully. I removed the application from device, and loaded using new set up only, problem again shows up. So what was there in old set up? What am I missing in new... ????
Refer to the documentation
To initialize your view controller object using a nib, you use the initWithNibName:bundle: method to specify the nib file used by the view controller. Then, when the view controller needs to load its views, it automatically creates and configures the views using the information stored in the nib file.
When initialising a view controller, and you're using a .xib file for the view, you need to call initWithNibName:bundle:. This means it'll use the xib file to create the view within loadView. At the moment, you're just using init, that will create a blank UIViewController object.
So in this case, your code would be (assuming the .xib is called "MainViewControllerView.xib" within the main bundle):
main =[[main_page_controller alloc] initWithNibName:#"MainViewControllerView" bundle:nil];
if (main) {
[root.navigationController pushViewController:main animated:YES];
}
Also sanity check your .xib file to see if all the IBOutlets are connected to what you want.
First check all your connection in xib(nib) file, if its already connected then just disconnect them , clean project (cmd+k) and then connect connection again.
take a look on this image for connection

UIView (subview) does not send IBActions to AppDelegate

I'm having this problem because I originially made everything in the main NIB, but then after reading learned that it is better to use subviews.
I've got my IBActions in the AppDelegate, and I've successfully connected and loaded my subviews. However, when I try to connect my buttons in the subviews to the IBActions in the AppDelegate, the IBActions appear under the "First Responder". They seem to connect fine, but when running the application they do not trigger the IBActions (I've confirmed this with some NSLogs, it's not an error in the code within the IBActions). What am I doing wrong?
Thanks!
The AppDelegate should only be used for very specific items such as implementing the UIApplicationDelegate protocol (i.e. methods like applicationDidFinishLaunching) or in some cases storing global variables.
You should keep IBActions and other outlets in their respective view controller files (i.e. if you created MyViewController.h and MyViewController.m which are linked with MyViewController.xib where you have some buttons, images, etc.). They can then be hooked up via dragging the inspector control you want (i.e. TouchUpInside) to the File's Owner.
Something you should read to better understand view controllers: http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Typically it is best to create a unique view controller for each view you will present to the user. For instance, if I had a main screen and then an "about" or a settings screen, I would make each of those their own view controller. It helps organize things better than using one view with a whole bunch of subviews that you hide/show and will also improve loading times and general performance.
Update for your 2nd question in the comments about accessing the app delegate:
First, you need to import the .h file (i.e. #import "AppDelegate.h") for the app delegate into whichever view controller .m file you wanna use to access whatever variables, arrays, etc you have stored in the app delegate files. Make sure you synthesize whichever objects you create in the app delegate's .h file in the app delegate's .m file so the getters and setters are created (so you can access them).
Then in the view controller .m file, in whichever method you are using:
-(void)someMethod {
// here we just create a shortcut to the app delegate called "theAppDelegate"
YourAppDelegateFileNameHere *theAppDelegate = (YourAppDelegateFileNameHere *)[[UIApplication sharedApplication] delegate];
// now you can use the dot notation if you wanna access a variable
int SomeNewInteger = theAppDelegate.someIntegerYouHaveStored;
// or some array you have stored
return [theAppDelegate.someArrayYouCreated count];
}
Hope that helps!

application: didFinishLaunchingWithOptions doesn't execute, but RootViewController: viewDidLoad does, how is this possible?

I'm playing around with the iPad SplitView template and it was working fine before I started swapping out view objects in my RootViewController. When it was working fine, the application:didFinishLaunchingWithOptions method would be called and would setup my persistant store objects, then the RootViewController:viewDidLoad method would be called to populate my rootView with data from my store. I opened up IB and started swapping out view objects in my RootView and now the application:didFinishLaunchingWithOptions method never gets called, but the RootViewController:viewDidLoad method still does. Obviously, the app crashes because the viewDidLoad method depends on the successful execution of the didFinishLauchingWIthOptions method to setup the persistent store objects. Does anyone have any thoughts on what is causing this or how I can go about investigating what's causing this?
I'm obviously new to iPhone OS development, so I apologize if this questions is absurd in any way. Thanks so much in advance for your help!
This is propabaly caused by the fact that in MainWindow.xib, your application delegate object is not connected to File's Owner (UIApplication). You can open the MainWindow.xib and right click on your App Delegate to see if it has a connection in Referencing Outlet to File's Owner. If not, set it to. And this will fix your problem.
-viewDidLoad is not called from -application:didFinishLaunchingWithOptions:. They are independent. The call hierarchy could be summarized as:
load app; call -application:didFinishLaunchingWithOptions:
window is visible, load views of view controllers.
call -viewDidLoad.

is applicationDidFinishLaunching the wrong place for setting an image for an UIImageView?

I found out this:
applicationDidFinishLaunching (an delegate method of the UIApplicationDelegate Protocol) seems to be called BEFORE my views from the nib file are loaded completely. So I tried all the day to change an image of an UIImageView right after my app launched in the iPhone simulator, but nothing happened.
Then I wrote a little action method that I call with the press of a button. And then it happened: WORKS!
So the applicationDidFinishLaunching delegate method isn't really the right place for stuff that has to be done after the app is really "ready". I gues there's something better that waits for the nib to be loaded completely. but where? and what?
I gues there's something better that waits for the nib to be loaded completely. but where? and what?
For application specific things like global settings, preferences, etc., -appDidFinishLaunching is the right place.
For UIView specific things, you typically use the -viewDidLoad method in a UIVIewController subclass. It is pretty much the only place you are guaranteed that the nib file is loaded, the IBOutlets are initialized and the IBActions are attached.
This is difference from the Mac OS X world, where -awakeFromNib was the place to do it.
Hey until your views and their view controllers instantiated you can't modify their ui. However just for the sake of your problem you can always declare the uiimageview as a property of your app delegate class and initialize it in the appDidFinishLaunching event. But that's the worst practise. As on the iPhone which has limited memory always lazy load ie: only initialize objects when and just before they are actually required by your UI. So ideally you should be doing this in the viewDidLoad event of the view where you want to use this UIImageView.
applicationDidFinishLaunching is usually used for stuff like database file checks, opening database connection, populating global variables, any other application wide logic, checking for an available Internet connection etc

Utility Application infoButton Stays on Top

I used a utility application template to create my application, but I am using a UIImagePicker to pick some photos. The little infoButton, the i with a circle around it, shows up when the picker is displayed. If the infoButton was not in another class then I could call infoButton.hidden = YES; and it would hide it. Any ideas how to solve this problem?
Thanks,
-Pat
I took a look at the utility application template, the view controller with a gray background and an info button is MainViewController, and the instance is also the "mainViewController" property of the application delegate.
It seems that you have already created an IBOutlet to the infoButton, so, you can access the button by calling [UIApplication sharedApplication].delegate.mainViewController.infoButton .