Is it possible to create and display a UITableView controller which allows the user to select an item and fire back a message to the delegate without subclassing it?
The reason is I just want to display a list of items in a popovercontroller and it seems a waste to have to create a subclass just for this
In the view controller that presents the popover you could implement UITableViewDataSource and UITableViewDelegate - then set the popover's tableview to use the parent controller as its source and delegate before presenting it.
If you are using iOS 5 SDK then you can make static cells.
Otherwise the only option is to create a subclass and provide a DataSource array.
Either ways you might want to have a View Controller that prepares the view controller loaded on the touch action?
Related
I am using a custom UITableView that complies to those protocols:
UITableViewDataSource
UIScrollViewDelegate
This table view is used in many places in my app. I have implemented image downloading in the background. When the user scrolls, additional images get downloaded – that's why I needed to implement the UIScrollViewDelegate methods. I definitely want to have this in this class and not in a view controller, because each of these custom table views use the same implementation of the methods.
Here is the problem. In my view controller, I need to implement the didSelectRowAtIndexPath method from UITableViewDelegate, because I need to perform a segue. But when I do that (and set the delegate to self), the UIScrollViewDelegate methods get caught by this view controller and are not propagated to the table view, because UITableView extends UIScrollView (and his delegate methods).
I have "solved" it by adding this to each of my view controller:
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[self.bookTableView scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
}
But having this same block of code (FYI this is only one of them, there are some more) on many places throughout the app isn't very good. Do you have any ideas how I could solve this?
I definitely want to have this in this class and not in a view controller, because each of these custom table views use the same implementation of the methods.
No, you do not. UITableView is a view. It should never talk to the network. That is the function of the model (or possibly a model controller). The function you're discussing has nothing to do with displaying information. It has to do with fetching information.
What you want is to the scroll view delegate methods into a custom UITableViewController and subclass your other table view controllers from that. The actual downloads, however, should be managed by your model. Your view controllers should observe the model and update as it changes.
I have solved it by using the new feature in iOS 6 – embedded view controllers. In each view controller, where I'm using this table view controller, I perform this:
self.tableViewController.tableView = self.bookTableView;
[self addChildViewController:self.tableViewController];
Where self.bookTableView is the UITableView in my view.
To perform a segue from this child TVC, you can use this:
[self.parentViewController performSegueWithIdentifier:#"Book Details" sender:selectedBook];
In my app i am using tabbar controller with 5 tabs,in 3 tabs,when click on a button it calls one View Controller. I am using same view controller for those 3 tabs so i am getting problem while calling same view in different tabs,So while changing the tab i dont want to call ViewWillAppear method. So what i have do? or else how to find previous selected index of the tabbar controller?
Thanks in Advance
I think you are not familiar with iOS Development.
In a TabbarController we can specify as much of UIViewcontroller/UINavigationController object.
if we need to re-use a UIViewcontroller you need to tag the view controller using a property object.
You need to use different instance of UIViewController for different tab.
While Showing a UIViewController its viewWillAppear got fired. we don't able to remove this behavior. But in most case we can handle such case with viewDidLoad (It is called once for an instance of view controller).
if we need to track the previously selected tab item we need to do our own work-around by using a shared class or static variable.
thanks,
Naveen Shan
In iOS5 using storyboard feature I want to create a custom container which will have 2 ViewControllers embedded in it. For Example, embed Table view controller as well as a view controller both in one ViewController.
That is, one view controller will have 2 relationship:
to table view controller
to view controller which in turn will have 4 UIImage view Or UIButton in it
Is creating this type of relationship possible using storyboard's drag drop feature only & not programmatically?
,You should only have one view controller to control the scene. However, this viewController might have two other view controllers that control particular subviews on your scene. To do this you create properties in your scene viewController, in your case one for your tableViewController and one for your view. I like to keep things together so I make both these viewControllers outlets and create them in interface builder. To create them in interface builder pull in an Object from the Object library and set its type to the relevant viewController. Hook it up to the appropriate outlet you just created in your scene's viewController - Note: this is important otherwise the viewController will be released if you are using ARC and crash your app. Then hook these viewControllers up to the view you want them to control and you are done.
Alternatively you can instantiate and hop up your viewControllers in your scenes viewController should you prefer to do this.
Hope this helps.
Edit: On reflection this is not a good idea and actually goes against the HIG you should maintain only one ViewController for each screen of content and instead try to create a suitable view class and have the single view controller deal with the interactions between the various views.
There is a way to do it that isn't too hacky. It is described at the following URL for UITabBarControllers, which you could use the first view controller in the list control the first subview, and the second one control the other. Or, you can probably adapt the code to work with UISplitViewController.
http://bartlettpublishing.com/site/bartpub/blog/3/entry/351
Basically, it works by replacing the tabbarcontroller at runtime after iOS has finished configuring it.
I have created an UIView in my iPhone app. I want to handle something when user closes or opens when UIView is present as current screen. I thought, i can do this under viewWillAppear:. But, viewWillAppear: is not called in UIView. Does it work only on UIViewController? How can i handle viewWillAppear: or viewDidAppear: for an UIView?
Update: UIView what I created everything through program, not in .xib.
Please advise.
Thanks!
From your message I infer that you wrote your viewWillAppear: method on the UIView class. As you suspect, that method is part of [UIViewController]1, not [UIView]2 therefore it only gets called on the UIViewController.
You should connect the property view of the UIViewController to the UIView object in the interface builder and then implement that method in the UIViewController.
If your view is created in response to an user action,
Update for your update:
You should tag the views either in code (view.tag=1) or IB.
Then you can do if (self.window.rootViewController.view.tag == 1) { ... } from your delegate (assuming you are looking for the view of the controller who is the rootController, otherwise post more details).
It's even better if you define constants on one place instead writing 1 as a literal.
These delegate methods are called every time the superview is presented to the screen and should be implemented in the UIViewControllers.
The gotcha is that these methods aren't called when subviews are presented on the screen, so your superview-view-controller will have to respond to these events accordingly.
You can find more information in this post here.
If you study the documentation for UIView and UIViewController what you will find is -(void)viewWillAppear:animated: is a method of UIViewController and not of UIView, so in order to use it, it must be implemented by subclassing UIViewController. Generally for best practice if you want to follow MVC, any functionality that does not pertain to the view itself should be delegated to the view controller and not be in the body of your UIView subclass.
Create a new view controller with xib file, and then link your custom view class to the view in your xib file.
I already have a view controller, is it possible to attach a table view on top of the current view, without the needed to create a subclass of UITableViewController?
Thanks
Yes, just set up the table view like any other view and connect it to your viewcontroller by setting the delegate (reference) and dataSource (reference) - properties. See the references on which method you have to implement in your viewcontroller in order to get things working.