UITableView content is gone - ios

I'm facing a strange behavior of a UITableView. I have a View Controller with a UIView, let's call it view A.
Inside UIView A I'm calling another view from a UIViewController, called view B.
HomeViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
UIView *B = [[BViewController alloc]init].view;
[self.A addSubview:B];
}
Inside B, I have a UITableView C.
BViewController.m
- (void) viewDidLoad
{
C.delegate = self;
C.dataSource = self;
[self.view addSubview:C];
}
This UITableView is loading correctly but when I just tap on the screen all of the table's content is gone. Just an empty table remains.

In your view controller B made property of type A and name it to delegate and set it with instance of A and set that delegate property to your table view' datasource and delegate instead of self. Because in viewdidload of B you are setting delegate and data source as self that means instance of B and your table view is being shown on viewcontrollet A. So A's instant would be the delegate of tableview. So set tableview's delegate and datasource to self in vc A or as i mentioned in above lines.Or you should add vc as childviewcontroller.

Following the official documentation of View Containers my problem was solved.

Related

problems with calling method of ViewController before calling viewController

I have two view controllers in Xcode project (all view controllers are created in storyboard).
First view controller has two (or more) buttons with certain background images. Second view controller should display full-screen background image of certain button after user touch it (certain button).
Second view controller has a property UIImageView that should be allocated and initialized in the code of second view controller (UIImageView not created in storyboard).
Second view controller is a delegate for first view controller and has a method:
-(void) viewController:(ViewController *) viewController buttonPressed: (UIButton *) button.
Every button has a modal segue to second view controller.
So the sequence of actions of application is next (I realized that by debugging):
User touches any button
Button calls an action method in which delegate method viewController:buttonpressed: is called. UIImageView instance is allocated and initialized in this method with the image returned by button backgroundImageForState:
Than method viewDidLoad of second view controller is called, in which UIImageView instance should be added to super view of second view controller and displayed on screen.
The problem is that despite of allocation of UIImageView instance in delegate method viewController:buttonpressed:, that instance is become nil at the start of method viewDidLoad of second view controller. All actions that been made in viewController:buttonpressed: became unavailing.
The code is below:
First View Controller Code
- (void)viewDidLoad {
[super viewDidLoad];
ViewControllerForImage *temp = (ViewControllerForImage *) [self.storyboard instantiateViewControllerWithIdentifier:#"ViewControlForImage"];
self.delegate = temp;
}
-(IBAction)buttonPressed:(id)sender{
[self.delegate viewController:self buttonPressed:sender];
}
Second View Controller Code
#synthesize myImage;
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:myImage];
}
-(void) viewController:(ViewController *) viewController buttonPressed: (UIButton *) button{
myImage = [[UIImageView alloc] initWithImage:[button backgroundImageForState: UIControlStateNormal]];
[myImage setFrame:CGRectMake(0, 0, 320, 504)];
}
Why don't you just pass the information in a prepareForSegueMethod?
Also in your code why are you sending an instance of viewcontroller back to the second view controller? You are not using it at all.

ViewDidLoad not called on custom objects in Storyboard

I finally made the switch to Storyboards and i am having issues loading custom controllers which was pretty easy to do when using interface builder.
I have ViewControllerOne with two components: A UIView and UITableView as the subview.
I want the UITableView to be controlled by a custom tableview controller. If this was Interface builder i would have dropped a tableview controller onto the XIB, linked to the custom controller and made the connections and it would have been done.
Using storyboard, i don’t believe its possible to drop a UIViewController/UITableViewController onto a scene which already has a view controller, i relied on Objects to achieve this.
So i added a Object onto the scene and linked it to my custom tableview controller. I set up delegate/date source for my UITableView to point to the custom controller. I finally connected the UITableViews outlet to the custom controller.
When i compile this, the custom controllers delegate (for the table view) gets called but the viewDidLoad is never called.
The only way i can invoke viewDidLoad is if i move the UITableView out of ViewControllerOne. My understanding was that even though there is one view controller for a scene i can still manipulate the subviews using custom controllers.
Am i misunderstanding something or is there is a solution for this ?
Some screenshots
There is a bit of magic in that. Call self.view from awakeFromNib and flow will back to the rails
- (void)awakeFromNib
{
[super awakeFromNib];
// here comes the magic - call self.view and view will load as expected
NSLog(#"awakeFromNib %#", self.view)
}
you can call it from initWithNibName:bundle:
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSLog(#"awakeFromNib %#", self.view);
}
return self;
}
the point is to call self.view because apparently something is done inside.
If I have understood your question correctly:
1 Open the storyboard and navigate to the table view controller that you would like to be of your custom type.
2 Click on the identity inspector in the right hand side panel.
3 Set the class to what it should be.

How to load another View when loading a ViewController

I have a ViewController it has 3 views. What I want to do is without loading the default view when loading the ViewController, load other view of the same ViewController (rarther than load the main view)
IS this possible. Then how I can do that?
Thanks
You'll have to call the addSubView method of the UIView class.
So, when your initial view loads in the viewDidLoad method of your UIViewController, you add another sub view to it.
[self.view addSubView : YOUR_CUSTOM_VIEW_HERE];
You have to give more precise description of your problem.As when you talk about a view, it can be a view like UIButton which can be added like
[Self.view addsubview:yourView];
But if you have created that view in some other custom class which is subclass of UIView ,then in your viewController.m
- (void)viewDidLoad
{
obj =[[customView alloc] init];
self.view =obj;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Where customView is a subclass of UIView whose view you wan't to load, not the view of the original viewController file.
What you are doing over here is that when the viewDidLoad method is called , you change the view of that viewController to view of the customView (subclass of UIView).
(adding view as addSubview is best option I think)

UICollectionView datasource not being called

Even though I am setting delegate and datasource, the data source methods are never being called.
I have a ViewController that adds a subview as such:
EVPhotoCollectionViewController *pc = [[EVPhotoCollectionViewController alloc] initWithNibName:#"EVPhotoCollectionViewController" bundle:nil];
self.damagePhotosView = pc.view;
Inside EVPhotoCollectionViewController I have delegate and datasource wired up in the xib, but also via code as follows:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.collectionView reloadData];
}
None of the datasource methods are ever called. I have verified self.collectionView is not null when it calls reloadData.
Thanks!
I think there are several things wrong here--and there are some complexities to view controller containment that you may need to read up on.
First off, you're not adding the EVPhotoCollectionViewController view as a subview of your vc, eg:
[self addSubView:pc.view];
Also, you're not setting a frame for the EVPhotoCollectionViewController, so depending on how it's implemented, it might not show up with the right size/position.
Lastly, it doesn't look like you're retaining the EVPhotoCollectionViewController anywhere. Its view will be retained by the view hierarchy, but it looks like the instance of EVPhotoCollectionViewController will be dealloc'd once the function creating it goes out of scope.
View controller containment: How does View Controller Containment work in iOS 5?

How delegation work if UITableViewController added to other UIView?

I want to present a UITableViewController in a UIView, I have a workaround, but I want to understand why it works this way.
This is how the scenario looks like:
- Created a new Single View Application
- In IB added a view to the main view
- Created an outlet for the view:
#property (weak, nonatomic) IBOutlet UIView *placeholderView;
- Added a new UITableViewController with XIB to the project
- Changed numberOfSectionsInTableView to return 1, numberOfRowsInSection to return 10, and cellForRowAtIndexPath:
cell.textLabel.text = #"hello";
- In ViewController file ViewDidLoad method:
ORGTableViewController *tableView = [[ORGTableViewController alloc] initWithNibName:#"ORGTableViewController" bundle:nil];
[self.placeholderView addSubview:tableView.tableView];
Now the table appears in placeholderView but, it's empty. In TableViewController file InitWithStyle, and ViewDidLoad methods are called. But none of numberOfSectionsInTableView, numberOfRowsInSection, and cellForRowAtIndexPath are called.
Also tried to add this code in AppDelegate to didFinishLaunchingWithOptions method, and it's —working as expected, cells with text are appear:
ORGTableViewController *tableViewController = [[ORGTableViewController alloc] initWithNibName:#"ORGTableViewController" bundle:nil];
self.window.rootViewController = tableViewController;
So it works, and it looks like delegation messed up somehow as I added UITableViewController to UIView, workaround is easy... ViewController, viewDidLoad method:
ORGTableViewController *tableViewController = [[ORGTableViewController alloc] initWithNibName:#"ORGTableViewController" bundle:nil];
tableViewController.tableView.delegate = tableViewController;
[self.placeholderView addSubview:tableViewController.tableView];
Do you know why delegation work this way?
THX!
Your question itself has the answer. In the first code, you were not setting the deletage where as in your second code, you are setting the delegate as tableViewController.tableView.delegate = tableViewController; which will call the delegate methods implemented in tableViewController.
If you are planning to implement delegate methods in your placeholderView class, you need to set the delegate as `tableViewController.tableView.delegate = self; and then implement all the delegates in placeholderView class. That would have worked.
If you just need a UITableView, you can also consider subclassing the UITableView class and add it as the subview of placeholderView.

Resources