UIViewController IBOutlets are nil - iphone

I have an UIViewController class with two labels and a UIImageView set as IBOutlets, and I have this outlets connected in my xib, I have double checked they are connected properly, however when I check the their value in the debugger they are 0x0 so I cant change them programatically. Any ideas on what I might be doing wrong.
Heres the header file of my code:
#import <UIKit/UIKit.h>
#interface PlateDetailViewController : UIViewController {
IBOutlet UIImageView *image;
IBOutlet UILabel *price;
IBOutlet UILabel *description;
}
#property (nonatomic, retain)IBOutlet UIImageView *image;
#property (nonatomic, retain)IBOutlet UILabel *price;
#property (nonatomic, retain)IBOutlet UILabel *description;
#end

Your outlets won't get set until the view controller's view is actually instantiated, which in your case is probably happening shortly after initWithNibName:bundle:—at which point they'll still be nil. Any setup you do that involves those outlets should be happening in your view controller's -viewDidLoad method.

Related

uiscrollview in uiview not scrolling

I am using the storyboard to create a UIView that contains a UIScrollView that has many child items in it. these child items consist of a UIImageView that I am using as a background image for the UIScrollView, a series of UILabels that I populate programmatically, and a series of UITableViews that I populate with data obtained from a called set of library classes. I want all of this in my UIScrollView so the User can scroll through all of the information without having multiple views being displayed. I have browsed through a lot of postings here and have used a lot of the suggestions, but have been unable to successfully have the view scroll and allow the user to see all the data I'm attempting to present.
Here's a screen shot of the Storyboard followed by a shot of the attribute inspector for my UIScrollView
I'll also provide the .h and .m files
weatherController.h
#import <UIKit/UIKit.h>
#import "WeatherParser.h"
#import "WeatherLocation.h"
#import "WeatherImg.h"
#import "WeatherWind.h"
#import "ForcastDay.h"
#import "ForecastItem.h"
#import "Condition.h"
#import "Atmo.h"
#import "Astro.h"
#class WeatherParser;
#class WeatherParserDelegate;
#class WeatherLocation;
#class WeatherImg;
#class WeatherWind;
#class ForcastDay;
#class ForecastItem;
#class Condition;
#class Atmo;
#class Astro;
#interface weatherController : UIViewController <UITableViewDataSource,UITableViewDelegate,WeatherParserDelegate>{
WeatherParser * _rssParser;
bool loadingEvents;
}
#property (nonatomic, retain) WeatherParser * rssParser;
#property bool loadingEvents;
#property (retain, nonatomic) IBOutlet UIScrollView *scrollView;
#property (weak, nonatomic) IBOutlet UILabel *cityLabel;
#property (weak, nonatomic) IBOutlet UILabel *state_countryLabel;
#property (weak, nonatomic) IBOutlet UILabel *currHighLabel;
#property (weak, nonatomic) IBOutlet UILabel *currLowLabel;
#property (weak, nonatomic) IBOutlet UILabel *currTempLabel;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet UIImageView *logoImageView;
#property (weak, nonatomic) IBOutlet UILabel *logoLabel;
#property (weak, nonatomic) IBOutlet UITableView *forecastTableView;
#property (weak, nonatomic) IBOutlet UITableView *atmoTableView;
#property (weak, nonatomic) IBOutlet UITableView *windTableView;
#property (weak, nonatomic) IBOutlet UITableView *astroTableView;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *sidebarButton;
#property (nonatomic, retain) NSMutableArray *forecastDays;
#property bool logoSet;
#end
and here's my weatherController.m file. I'm going to only add the viewDidLoad method which is the only method containing code regarding my UIScrollView.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_scrollView.delegate = self;
// self.scrollView.frame = CGRectMake(0, 0, 320.0f, 480.0f);
self.scrollView.contentSize =CGSizeMake(320.0F, 480.0F); //(320, 1210)];
[_scrollView setScrollEnabled:YES];
}
I would appreciate any help regarding what it is I'm either doing wrong or missing. If I set up my frame and contentSize to the following:
self.scrollView.frame = CGRectMake(0, 0, 320.0f, 480.0f);
self.scrollView.contentSize =CGSizeMake(320.0F, 1210.0F);
I do get the UIScrollView to scroll, but the image for the background is only the size of the frame, and none of the rest of the child content on the UIScrollView is displayed. It seems like it's allowing me to scroll but I'm only able to view the content that is viewable within the frame.
Again, thank you in advance for any help offered.
As the comments are suggesting, it's generally a bad idea to put a UITableView within a UIScrollView. I won't be too hard on you though, because there is a time and place where it is an acceptable solution. I'll let you judge if this time is now or not.
For reasons I still don't fully understand, UIScrollView behaves more like you expect it to if you enclose the entire contents in a single UIView that is as big as you want the UIScrollView to scroll.

What is the owner of UI elements of a view controller? iOS5 with ARC

I am new to iOS 5. From Apple's documentation I know what ARC is and "Owner of an object should using strong notation." After read "Hello World" , I noticed a strange thing.(I mean that was confusing me)
HelloWorldAppDelegate:
#interface HelloWorldAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
HelloWorldViewController:
#import <UIKit/UIKit.h>
#interface HelloWorldViewController : UIViewController <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
- (IBAction)changeGreeting:(id)sender;
#property (copy, nonatomic) NSString *userName;
#end
Here:
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UILabel *label;
UI elements have weak notation and none of file have a strong references to them. So I am confusing what/who hold them?
In my opinion you can consider the NIB/XIB as the owner of those objects. None of your classes own them. See "Managing the Lifetimes of Objects from Nib Files" in Resource Programming Guide:
From a practical perspective, in iOS and OS X outlets should be defined as declared properties. Outlets should generally be weak, except for those from File's Owner to top-level objects in a nib file (or, in iOS, a storyboard scene) which should be strong. Outlets that you create should will therefore typically be weak by default, because:
Outlets that you create to, for example, subviews of a view controller's view or a window controller's window, are arbitrary references between objects that do not imply ownership.
The strong outlets are frequently specified by framework classes (for example, UIViewController's view outlet, or NSWindowController's window outlet).

Issue Subclassing UIView

I've created my own custom view, with its own header and main file and corresponding nib (.xib):
The header file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface PointsBarView : UIView
{
IBOutlet UIView *pointsBarView;
IBOutlet UIView *pointsCounterView;
IBOutlet UILabel *pointsTotalLabel;
UIImageView *handImageView;
UIImageView *powerBarOutlineImageView;
}
#property (nonatomic, retain) IBOutlet UIView *pointsCounterView;
#property (nonatomic, retain) IBOutlet UIView *pointsBarView;
#property (nonatomic, retain) IBOutlet UILabel *pointsTotalLabel;
#property (nonatomic, retain) IBOutlet UIImageView *handImageView;
#property (nonatomic, retain) IBOutlet UIImageView *powerBarOutlineImageView;
#end
I'm synthesizing everything in the main, and then in another UIViewController class I'm trying to load this view. I set up its property:
#property (nonatomic, assign) IBOutlet PointsBarView *pointsBarView;
And am adding as so:
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"PointsBarView" owner:self options:nil];
pointsBarView = [nibViews objectAtIndex: 0];
[[self view] addSubview:pointsBarView];
How do I access those subviews within my NIB? Do they need to be embedded within the pointsBarView? (pointsBarView references the main view of the NIB, and all the other views are within the pointsBarView). Should they each be a separate piece of the NIB and I need to call addSubview for each one to display?
I should note that if I do NOT connect any of the properties in PointsBarView, the view displays just fine with the code in my UIViewController class. But I want to be able to interact with each view and change properties accordingly. Any help would be great!
The general rule of thumb is: if you load it from code, you connect it up in code.
conversely:
If you instantiate in IB, you can connect outlets and actions in IB.
Here you are loading the view in code, so you have to manually connect them.
If you want to be able to connect stuff in IB add a UIVIew in IB and change the subclass to PointsBarView. IB will magically read the PointsBarView.h file and you should be able to connect outlets, targets and actions.

is not a kind of ... as specified by the outlet type

I'm using a derived UISearchBar because I have to set the font size:
#import <UIKit/UIKit.h>
#interface SearchBar : UISearchBar
{
UITextField *searchField;
}
#property (nonatomic, retain) UITextField *searchField;
- (void) setFontSize: (NSInteger) size;
#end
In my ViewController the Outlet is defined:
#interface DictViewController : UIViewController <UIApplicationDelegate, UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *myTableView;
IBOutlet SearchBar *mySearchBar;
NSMutableArray *searchResults;
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
#property (nonatomic, retain) IBOutlet SearchBar *mySearchBar;
...
InterfaceBuilder don't let me connect the SearchBar to mySearchBar.
If I change the outlet class to UISearchBar, IB let me connect to mySearchBar. When I change the class back to SearchBar, the connection remains, but with the warning "The 'mySearchBar' outlet of FilesOwner is connected to 'SearchBar' but 'UISearchBar' is not a kind of 'SearchBar' as specified by the outlet type"
What's wrong? Any idea what I can do?
This might fix the problem.
In IB, select the search bar and go to the Identity pane of the inspector window. In the class dropbox, choose SearchBar instead of UISearchBar.

iPad/iPhone TableVIew help pushing a new view

So we have recently started developing applications for the iPad for our company. Unfortunately none of us here have ever done any iPhone/iPad development so we are just kind of learning on the fly and winging it. Basically our problem is happening when we try to push a view onto the screen when a table row is clicked. Neither of us can figure out why it's not doing it because the code looks 100% correct by anything we can tell. Here is what the didRowSelectedAtIndex: method looks like:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic -- create and push a new view controller
if(pdvController == nil)
pdvController = [[PersonDetailViewController alloc] initWithNibName:#"PersonDetailView" bundle:[NSBundle mainBundle]];
Person *aPerson = [appDelegate.people objectAtIndex:indexPath.row];
pdvController.aPerson = aPerson;
[self.appDelegate.navigationController pushViewController:pdvController animated:YES];
}
and here is the header for the toolbarSearchViewController:
#import <UIKit/UIKit.h>
#import "RecentSearchesController.h"
#import "PersonDetailViewController.h"
#class ToolbarSearchAppDelegate, PersonDetailViewController;
#interface ToolbarSearchViewController : UIViewController <UISearchBarDelegate, UIPopoverControllerDelegate, RecentSearchesDelegate> {
ToolbarSearchAppDelegate *appDelegate;
UIToolbar *toolbar;
UISearchBar *searchBar;
UITableView *resultsTable;
PersonDetailViewController *pdvController;
RecentSearchesController *recentSearchesController;
UITableViewController *resultsTableController;
UIPopoverController *recentSearchesPopoverController;
}
#property (nonatomic, retain) IBOutlet UIToolbar *toolbar;
#property (nonatomic, retain) UISearchBar *searchBar;
#property (nonatomic, retain) IBOutlet UITableView *resultsTable;
#property (nonatomic, retain) ToolbarSearchAppDelegate *appDelegate;
#property (nonatomic, retain) PersonDetailViewController *pdvController;
#property (nonatomic, retain) UITableViewController *resultsTableController;
#property (nonatomic, retain) RecentSearchesController *recentSearchesController;
#property (nonatomic, retain) UIPopoverController *recentSearchesPopoverController;
#end
and the ToolbarSearchAppDelegate header:
#import <UIKit/UIKit.h>
#class ToolbarSearchViewController;
#interface ToolbarSearchAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ToolbarSearchViewController *viewController;
UINavigationController *navigationController;
NSMutableArray *people;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ToolbarSearchViewController *viewController;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) NSMutableArray *people;
#end
and finally the personDetailController.h:
#import <UIKit/UIKit.h>
#class Person;
#interface PersonDetailViewController : UIViewController {
IBOutlet UITableView *tableView;
Person *aPerson;
}
#property (nonatomic, retain) Person *aPerson;
#property (nonatomic, retain) UITableView *tableView;
#end
We are getting pretty annoyed because neither of us can figure out why it's not loading and any help or insight you guys could provide would be amazing. I wasn't sure what you may need to see, so if you need anything more specific, let me know and I will post it. Thanks!
ps. I am not sure how the code tags are going to handle all of Obj-c's quirky syntax, so if you see syntax errors, just ignore them. Assume that it is syntactically correct and will compile and run...
This line looks a little odd:
[self.appDelegate.navigationController pushViewController:pdvController animated:YES];
try and make it
[self.navigationController pushViewController:pdvController animated:YES];
edit summing op, the problem is that you don't have a navigation controller, so you can't push a view controller like that. What you can do, is pushing a view controller modally, using
[self presentModalViewController:pdvController animated:YES];