Using UIRefreshControl with UISearchBar search results - tableview

I'm trying to get a behavior like the Mail app where there is a UISearchBar and you can pull to refresh the search results. Currently, I have the Search Display Controller with the UISearchBar connected in IB and everything works fine (I can refresh when in the main tableview and I can search).
But when I search and try to pull to refresh the search results (like the mail app), there is no UIRefreshControl. Check the video below:
Screen Recording
I guess I have to make the UISearchBar the headerView of the tableView but I can't reference the UISearchBar since it's an outlet of the Search Display Controller in IB. Here is my code for the UIRefreshControl :
//Pull to Refresh
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(pullToRefresh)
forControlEvents:UIControlEventValueChanged];
[self.carTableView addSubview:refreshControl];
I'm using the above code in the -viewDidLoad method of the UIViewController that contains the tableView

I solved the problem by creating another UIRefreshControl and adding it to the UISearchDispayController every time the user type a search text in the search bar which I can detect in - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView . Here is the code:
//in viewDidLoad
//Pull to refresh in the search
searchRefreshControl = [[UIRefreshControl alloc] init];
[searchRefreshControl addTarget:self action:#selector(pullToRefresh) forControlEvents:UIControlEventValueChanged];
Then:
//in - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
//add the refresh control to the search display controller tableview
[self.searchDisplayController.searchResultsTableView addSubview:searchRefreshControl];

Related

Trying to make a tabbed browser

I'm having a problem with cocoa, when I run the app the tabs get added as expected, but all the web views take the same string from the url field.
Basically, if I go to google on one tab, it goes to google on all of them.
Is there any way to make only the web view on the selected tab respond, and not on the others?
Here is the code:
- (IBAction)newTab:(id)sender {
NSTabViewItem *item = [NSTabViewItem new];
[item setView:_webView];
[item setLabel:#"New Tab"];
[_tabView addTabViewItem:item];
}
It looks like you're creating a new tab and then moving your WebView to it. To create a new web view for each tab, you have some options but one is to use NSViewController:
If you create the web view in the same xib as the tab view, move it to a separate xib. Change the class of the owner of that xib to NSViewController.
When adding a new tab in your code, load the xib (assuming it is named WebView.xib):
- (IBAction) newTab: (id) sender
{
NSTabViewItem *item = [[NSTabViewItem alloc] init];
NSViewController *viewController =
[[NSViewController alloc] initWithNibName: #"WebView" bundle: nil];
WebView *webView = [viewController view];
[item setView: webView];
[_tabView addTabViewItem: item];
[_webViewControllers addObject: viewController]; // Store the view controller, remove when the user closes the tab.
}
Here's a tutorial on view controllers: http://comelearncocoawithme.blogspot.fi/2011/07/nsviewcontrollers.html

Switching of one view to another view in single customViews

I am having of one CustomView in one xib and two different views in two different xib's.
I want to display those two view one after the other in one single CustomeView.
I have an object of NSView which is connected to CustomView in .xib file
#property (retain) IBOutlet NSView *mySubview;
#property (retain) NSViewController *viewController;
Method to open one View is:
-(IBAction)selectBookTicket:(id)sender
{
//setting status label to nil
_viewController=[[NSViewController alloc] initWithNibName:#"BookTicket" bundle:nil];
//loading bookTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
Method to open another view in same CustomView is:
-(IBAction)selectTicketCancellation:(id)sender
{
_viewController=[[NSViewController alloc] initWithNibName:#"CancelTicket" bundle:nil];
//loading CancelTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
When I am opening any view for first time its displaying properly in CustomView, but when I am trying to open second view or same view for second time then its get overlapping on previous opened view.
I tried
[_mySubview removeFromSuperview]
It's removing 'mySubview' completely, I mean what ever the view is currently loaded it is get removing but it's not allowing to display any views after that '[_mySubview removeFromSuperview]' get executed.
You must remove only the view that you have added from the view controller. Try the following code instead.
-(IBAction)selectTicketCancellation:(id)sender
{
[[_viewController view] removeFromSuperView];
_viewController=[[NSViewController alloc] initWithNibName:#"CancelTicket" bundle:nil];
//loading CancelTicket xib in custom view of NormalUserWindow
[_mySubview addSubview:[_viewController view]];
}
Executing [_mySubview removeFromSuperview] will remove your host view (i.e; the one that is displaying views from other view controllers) from view hierarchy and this explains the "not allowing to display any other subviews part".

customized backbutton working like real back button xcode

i am making a customized back button which works exactly as a real back button does. below is my code
UIButton *cusBack = [UIButton buttonWithType:101];
[cusBack setTitle:#"Back" forState:UIControlStateNormal];
[cusBack addTarget:self action:#selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
self.view addSubView:cusBack;
and here is my selector:
-(void)clickBack{
PrevPage *pPage = [[PrevPage alloc] init];
[self.navigationController pushViewController:pPage animated:YES];
[pPage release];
}
actually, it works but the direction it creates is from RIGHT to LEFT. but I want the page to move from LEFT to RIGHT since it goes back to the recent page
thank you!
If you want to have the same behavior as the back button you should try another approach:
-(void)clickBack{ //Your presenting view controller will always be the last view controller on the stack.
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-1] animated:YES];
}
Also, if you need to do some changes to the previous view controller, all you need to do is import it's header file and then you can use it's properties and public methods before popping the view controller.
#import "MyPreviousViewController.h"
...
-(void)clickBack{
[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-1].title = #"Change title for previous view controller";
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:self.navigationController.viewControllers.count-1] animated:YES];
}
Currently, your back button doesn't work as the real back button does, because, instead of popping the last view controller off the stack(also releasing the objects), you actually keep pushing new view controllers on the stack. You might have some issues if this stack gets too big.

How do I put the name of the IBAction pressed on my main view controller in a NSString on my modal view?

I have about ten IBActions on my main view that go to the same modal view and I need to know how to see what IBActon was pressed to get there and put that in a NSString on the modal view.
Here's the code that sends you to the modal view. The only difference in the IBActions is the name. Like playMovie: and playMovie2: and so on.
-(IBAction)playMovie:(id)sender{
VideoSubViewController *subView = [[[VideoSubViewController alloc] init] autorelease];
[subView setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:subView animated:YES];}
You could create an init* on the modal view that takes that information. The usage would be:
VideoSubViewController *subView =
[[[VideoSubViewController alloc] initForAction:#"playMovie"] autorelease];
You then store that information in the view instance in order to use it.

Push a view from a custom table view cell Xcode 4

I was wondering if there was a way to have the user create/delete cells in a Table View but when they click on it, every cell that they create loads the same view. I have the first part created but I cannot seem to get it to load a view.
Simple create a new view and push on didSelectRowAtIndexPath Delegate method of UITableView.
For ex.
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
DetailViewController *vController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:nil];
[self.navigationController pushViewController:vController animated:YES];
[vController release];
}

Resources