Unable to link UITable delegate to File Owner in IB of xcode - iphone

I am facing an issue and that is -
I am using tabBarCpntroller in my application. In this there are just two views. Both the views have tables in that.
I am able to display and populate the table in the first view but as I try to connect the delegate of UITable in the IB for the second view and then run the code, it crashes.
PS: I am using the same code as in first view.
Kindly help me out with the suggestions, I would be highly grateful
Thanks in advance!!

By the looks of your question, you need UITableView, not UITable, if this is what you are using, then you will have problems.
in your header, you must define your UITableView as an IBOutlet.
.h
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *myTable;
}
#property (nonatomic, retain) UITableView *myTable;
#end
Once you have done this, build (only) your code, then Interface Builder will see that outlet and you can connect it.

Related

Add subview not working?

I have two xib files:
MainView.xib and DetailView.xib
Both are controlled by MainViewController. MainView.xib loads when the app first opens, but if a user clicks on a button, the app loads DetailView.xib as a subview.
DetailView should load because I made an IBOutlet in the MainViewController to the view in the DetailView.xib file.
I am trying to use the addSubview command, but for some reason it is not actually executing the command. It will go through the command, but nothing will actually change. Here is the command:
[self.view addSubview:myDetailView]
where myDetailView is the IBOutlet
What is wrong with this setup?
Thanks for the help.
EDIT:
MainViewController.h (left generated code out):
IBOutlet UIView *myDetailView;
#property (nonatomic, retain) IBOutlet UIView *myDetailView;
MainViewController.m:
#synthesize myDetailView;
NSLog myDetailView before you add the subview, and if it returns "(null)" then the myDetailView has not been initialised. Make sure that you have connected the view in Interface Builder.
I bet myDetailView is nil.
It may be an IBOutlet, but it has to be connected. And the XIB in which you connected the IBOutlet should obviously be loaded.
How did you load the DetailView.xib in your code? Did you use loadNibNamed:owner:options:?

Cant load UITableView

Ok, this is really really easy, but even though I went through 100s of tutorials, I still have no idea whats wrong with my app. All I want to do is just to display a table view, even empty, but I get a black screen on the simulator and an exception in the output too.
This is what I did (followed step by step a few tutorials):
Open a view based app
This is my header:
#interface TableViewsViewController : UIViewController <UITableViewDelegate> {
IBOutlet UITableView *tblSimpleTable;
}
#property (nonatomic, retain) IBOutlet UITableView *tblSimpleTable;
#end
did not do anything in .m besides synthesizing.
IB: made 3 connections: delegate, dataSource, and tblSimpleTable to File's owner.
Yes, I am a beginner, but this is ridiculous...appreciate any help. Thanks!
First off, a table view has two "delegate" types -- the table view delegate and the table view data source. Your interface is being a delegate, but not a data source, for the table view.
If you add UITableViewDataSource to the interface, i.e.
#interface TableViewsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
and then compile, you'll probably get errors about missing methods for number of sections and for cell. After you add these to your implementation, the table view should work.
My guess is the table view tries to ask your class for number of sections in the table, and since your class doesn't actually respond to that selector, the code crashes.
Add
tblSimpleTable.delegate = self
in viewDidLoad
...and ensure you have connected it correctly in interface builder.

In IB, I created a table and now want to assign a variable to it

I now want to refer to the table, but there isn't a variable in my .h file as it wasn't required when originally created. The table works great, can add/display/etc.
I tried adding "myTable" to the .h and then linking in IB (the table was linked to "view" and I could/did select "myTable"), but my first try didn't work and I was afraid of messing up my (somewhat) working app ;-)
Hope the question makes sense!!!
thx!
Simply use
IBOutlet UITableView * myTableView;
in your .h file i.e Declaration file.You can now access myTableView in IB.You can now create the object for this TableView.
If I understand you correctly, you want to link the table with your code. If that's the case, then in your header file you need to add a property like so
#property(nonatomic, retain) IBOutlet
UITableView *myTable;
And then synthesize it in your .m file like so
#synthesize myTable;
Once you've done that you will be able to link it up in interface builder. But bare in mind that if you want to populate/interact with the table further you need to set the data source/delegate of your table to your file's owner. Here is a tutorial on how to do that - http://www.switchonthecode.com/tutorials/how-to-create-and-populate-a-uitableview
Does that answer your question?
If your controller is a UITableViewController, just use its tableView property.
The question doesn't entirely make sense because it's not clear what "the table works great..." means if you don't have any reference to it in your .h file. I'll assume you do have an iVar of class UITableView that is defined as an IBAction and you've implemented the table view dataSource and delegate protocols in your view controller implementation. If you have not then the table cannot display any data.
First, if you have an iVar defined you can just refer to the iVar directly. If you prefer a property you can create one.
If your view controller is a UITableViewController you don't need to do anything, the property tableView is already defined for you and you can refer to the table with the tableView property.
If your view controller is something else, you can create a property as follows in your .h file:
#property (nonatomic, retain) UITableView * myTableView;
and in your .m
#synthesize myTableView;
You can then refer to your tableView with:
self.myTableView;
Replace myTableView with the name of your tableView iVar. If you need to connect your tableView in IB to the myTableView property then instead define the property as:
#property (nonatomic, retain) IBOutlet UITableView * myTableView;
Once you've done this, edit your UI in IB and connect the tableView outlet to the myTableView property. You will also need to assign the tableView dataSource and delegate correctly. This is getting a bit beyond your question. Apple's Table View Controller Programming Guide has great docs on this.

iPhone Subclassing view controller and IBOutlet UITableView

I have the following problem. I've created a ViewController pretty much like the above
#interface MyViewController : UIViewController {
IBOutlet UITableView *myTableView;
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
I've linked myTableView on the Interface Builder to the matching nib's UITableView. and I've subclassed MyViewController to create YourViewController as so:
#interface YourViewController : MyViewController {
}
And then I load from a TabBarController the YourViewController on a tab item. Although I can see that MyViewController is indeed invoked at the end, no table view is displayed on the emulator.
I've tried debugging the MyViewController and it appears the the IBOutlet is nil.
Why is that?
I have encountered major issues with inheritance and IBOutlets and IBAction. I advise you to avoid that, and create shared stuff in another way.
I was hit hard by bugs when getting memory warnings for instance. Outlets and actions didn't get reconnected properly when they were defined in base class vs derived class.
Probably MyViewController's nib file is not loaded at all. Are you using for YourViewController a specific nib file? and in which way are you creating YourViewController.
Can you also try to define an "awakeFromNib" method in YourViewController and then from it call [super awakeFromNib] ?
However to understand your issue you must clearly explain how you load objects and if and where you use Nibs?
If you add it dynamically using code then only it will work. Not using Nib.
the UITableView (ie. your myTableView) needs delegates for data source and and its control.
And your controller needs a link to the Table view in the xib.
declare table delegate protocols in the interface section of your controller.
using IB, link the TableView in your xib to owners file:delegates.
using IB, link the file owner myTableView to the tableView in the xib.
I hope it will help.
Assuming that you have your whole navigation stack in MainWindow.xib
Open MainWindow.xib
Select YourViewController (Or whatever your controller that is subclassing UITableViewController is called)
Select Attributes Inspector
Ensure that the 'NIB Name' property has your correct YourViewController (Or whatever the name) selected
I had this exact same issue, this solved it for me.

Objective-C Novice. Change property in Controller from another Controller?

The context: I have three views. One Introductory view, an Upload view and the Main view. As classes (With their respective headers) I have the rootViewController (SwitchViewController), IntroViewController and UploadViewController. The first view to be shown is IntroView. The user presses a button (declared in SwitchViewController) that takes them to the UploadView, then in the UploadView they get to choose an image and press the button again to go back to IntroView.
The thing is that while the user gets to pick the image with UIImagePickerController the button to switch views won't hide nor a UIImageView I have with a logo on top of the view(screen). The UIImageView and the UIButton are both declared in SwitchViewController's header.
The code used:
UploadViewController.h
#import [...] //Imports
#class SwitchViewController;
#interface UploadViewController :
UIViewController <UIImagePickerControllerDelegate,
UINavigationControllerDelegate,UIActionSheetDelegate> {
UITextField *imageTextField;
UIImageView *uploadedImage;
SwitchViewController *switchViewController;
[...]
}
#property (nonatomic, retain) SwitchViewController *switchViewController;
#property (nonatomic, retain) IBOutlet UITextField *imageTextField;
#property (nonatomic, retain) IBOutlet UIImageView *uploadedImage;
[...]
#end
UploadViewController.m
[...]
- (IBAction) selectImageButtonPressed {
self.switchViewController.submitButton.hidden = YES;
self.switchViewController.imageLogo.hidden = YES;
[...] //continues
I just begun recently programming in objective-c so please forgive me if the question is very essential. I have looked and am following "Beginning iPhone 3 Development" of APRESS. But even if it helps to greatly understand the basics sometimes I get lost.
PS: If it is clearer to answer the question the SwitchViewController.h and .m snippet codes can be provided if asked. But I thought this text is big as it is.
#Joze i think I may have understood your problem switchViewController is a variable of the class UploadViewController so if you do anything with that variable it wont affect the switchViewController view. so when you are calling the switchViewController view at that time you have to do initWithNibName: bundle: and then hide the button and imageView and also you need to do something like switchViewController.delegate = self; and then call the view modally or what ever way you want it.
PS. i m not sure the that spelling is correct. i dont have xcode at my home.
I hope your problem solves with this.
I solved my problem after refactoring the whole code and changing the general structure of the program itself. Now I have 3 views and each with a viewController to control it. All the switching of views occurs in the Delegate since he has access to everyone. That way I can control every property with every controller, without much difficulty. Changing the property of one of the objects present in one view from another view is difficult and rather inconvenient if not sometimes impossible.
The approach I took when asking this question was short sighted for the application that had to be done. I thank all those who tried to help.