How to set scrollView delegate from a UITableView? - iphone

I have a UITableViewController that contains a tableView. I want to set its scrollView delegate to self, but I am not sure how to do that? if i call
self.tableView.delegate = self;
It will set the UITableViewDelegate to self, and not the UIScrollViewDelegate

A table view delegate is a scroll view delegate, so you have already set it.
#protocol UITableViewDelegate<NSObject, UIScrollViewDelegate>

Related

UITableView - get notification about scrolling

Is there any way to know, when the UITableView scrolled ? I need something that is the same like scrollViewDidScroll in scrollview.
If you set your UITableView's delegate to self, the view controller should call scrollViewDidScroll() when the view is scrolled:
myTableView.delegate = self
This works because UITableView is a subclass of UIScrollView.

How UIActivityController is presented?

I am trying to present a custom size UIViewController on top of UITableViewController without dismissing the UITableViewController but I could not. All I could do was to add the view of the UIViewController as subview.
The UIActivityController seems to be working differently when it is presented. The presenting view controller is not set to nil.
How can I present a custom size UIViewController -as the UIActivityController- without dismissing the presenting view controller ?
Thank you
Here's something you can do:
ActivityViewController *activity = [[ActivityViewController alloc]init];
[self addChildViewController:activity];
[self.view addSubview:activity.view];
Here, ActivityViewController is your custom UIViewController and inside its viewDidLoad or so, you can set its frame to the height you like. Or you can have a method which can be used to vary the height according to the number of buttons you have inside that UIView

iphone development: how to put a UIView over UITableView [duplicate]

This question already has answers here:
How to add a UIView above the current UITableViewController
(21 answers)
Closed 8 years ago.
In my app I use storyboard. One of my elements in the storyboard is a UITableViewController. So it has a tableview inside of it.
My question is how can I put a UIView over this tableview. It is gonna be hidden and I want to make it visible when a tableviewcell in the tableview is pressed. Is that possible? How can I do that?
The best solution is use normal view controller (UIViewController) in StoryBoard. Your controller will need to implement two protocols (UITableViewDataSource and UITableViewDelegate) and you will need add UITablewView in the view controller's view.
In this case in interface builder you will be able to put any view in the view controller's view (can put it above table view).
Use tableHeaderView property.
Returns accessory view that is displayed above the table.
#property(nonatomic, retain) UIView *tableHeaderView
The table header view is different from a section header.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html
Lets assume your view to be hidden/shown on top of table view is topView, declared as a global variable.
Declare topView in .h as
UIView *topView;
Now Lets assume that you have the UITableViewController object as tableViewController then, initialize the topView in viewDidLoad of tableViewController class
-(void)viewDidLoad
{
topView=[[UIView alloc] initWithFrame:yourNeededFrameSize];
[self.tableView addSubview:topView];//tableView is a property for UITableViewController inherited class
topView.hidden=YES;//Hide it initially for the first time.
}
assuming that you have the UITableViewDelegate methods implemented here is what you will do in didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(topView.isHidden==YES)
{
topView.hidden=NO;
}
else
{
topView.hidden=NO;
}
}
hope it helps.
you can also get view into front.
[view bringsubviewtofront];
I had a similar problem where I wanted to add a loading indicator on top of my UITableViewController. To solve this, I added my UIView as a subview of the window. That solved the problem. This is how I did it.
-(void)viewDidLoad{
[super viewDidLoad];
//get the app delegate
XYAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
//define the position of the rect based on the screen bounds
CGRect loadingViewRect = CGRectMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2, 50, 50);
//create the custom view. The custom view is a property of the VIewController
self.loadingView = [[XYLoadingView alloc] initWithFrame:loadingViewRect];
//use the delegate's window object to add the custom view on top of the view controller
[delegate.window addSubview: loadingView];
}

UITextViewDelegate: how to set the delegate in code (not in IB...)

Suppose you have an UITextView and you would like to set the delegate of this UITextView.
First thing is that you put this in your header file:
#interface myViewController : UIViewController <UITextViewDelegate> { ...
Then, if you were using IB, you would click the UITextView, and connect the delegate outlet with File's Owner. This would allow you to use commands such as - (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView { etc.
Now, suppose you wanted to do this programatically. I found this suggestion on here:
textView.delegate = yourDelegateObject;
But I have no idea what 'yourDelegateObject' stands for. In IB, I am connecting with File's Owner... so in code, it would need to be textView.delegate = File's Owner. But what is the File's Owner in this case? myViewController? UIViewController?
I don't really understand the principle, I suppose. Any help would be very much appreciated.
As others have stated, set the delegate property of the UITextView. The most common delegate object is self.
To elaborate on "delegate object": the delegate object is the class (implementing the UITextViewDelegate protocol) that you want the events to respond to. For example, if you use a class instance instead of self, the UITextView will send its events to the implementations of the delegate methods in that class.
Most likely you want to assign a delegate to a current controller object, that is self:
textView.delegate = self;
yourDelegateObject = self.
you can use
textView.delegate = self;
The delegate is your UIViewController so it's
textView.delegate = self;
Try to put self as delegate:
textView.delegate = self;
So you need to put the function - (BOOL)textViewShouldBeginEditing:(UITextView *)aTextView in the implementation of your controller.
The delegate can be any object, it doesn't have to be the class the textField is created in, though usually it is - whenever this is true you will set it to self, though you can set it to any instanced object that conforms to the protocol (whenever a formal protocol is defined for the object).
Just assign it to self:
textView.delegate = self;
Swift:
textView.delegate = self
Make sure in your class you add UIItextFieldDelegate.
Example: say my class was called Pizza and my textField was called goodEats
class Pizza: UIViewController, UITextFieldDelegate {
//Then set the delegate in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
goodEats.delegate = self
}
}

loading table view on click of button

I have created an iPhone application with using the view based template; the problem is I want to load a table view on the click of button. I have tried this:
initWithNibName:#"xibfile" bundle:[NSBundle mainBundle]...
but table view is not loading, what should I do?
Assuming you are using a navigation controller, in the code related to the action associated to your button, do something like
YourTableViewController *yourTableViewController = [[YourTableViewController alloc] initWithNibName:#"YourTableViewController" bundle:nil];
[self.navigationController pushViewController:yourTableViewController animated:YES];
[yourTableViewController release];
Otherwise, you may present your table view controller as a modal view.
If you're asking how to load a view controller, the pattern looks something like this:
MyViewController* myViewController = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
If you're not using or directly subclassing UITableViewController I'm assuming that your UITableView is an ivar of your UIViewController subclass.
If this is the case you'll need to assign your UIViewController subclass to be the UITableView's delegate and set your UIViewController to conform to the UITableViewDelegate protocol. Also you'll need to create a datasource object that conforms to the UITableViewDataSource protocol. Then set the UITableView's data source to be that UITableViewDataSource object.
So in your header file you'll have:
#class SomeDataSource;
#interface{
UITableView *tableView;
SomeDataSource *dataSource;
}
Then in your implementation block, probably in viewDidLoad, you should include:
tableView.delegate = self;
tableView.dataSource = dataSource;