How to create dropdownlist without using UIPIckerviewcontroller in IPhone? - iphone

I want to dropdownlist like listview at when i click the dropdown button and make the list have some contents. then any content i select it will be text for the label any one help me. Thanks Lot.

You can use pop-over to display list.In pop-over you can create tableview to display list of items and when user selects any option, didSelectRowAtIndexPath will be called, from this method you can send the selected value and display in label.
Code in mainviewcontroller, where you want to display drop down.
if (m_OptionController !=nil)
{
[m_OptionController release]; m_OptionController = nil;
}
m_OptionController=[[OptionViewController alloc]init];
[m_OptionController setTarget:self andSelector:#selector(displaySelectedOption:)];
if(m_pPopOverController)
{
[m_pPopOverController dismissPopoverAnimated:YES];
[m_pPopOverController release];
m_pPopOverController=nil;
}
m_pPopOverController=[[UIPopoverController alloc]initWithContentViewController:m_OptionController];
[m_pPopOverController setPopoverContentSize:CGSizeMake(thePopOverFrame.size.width, thePopOverFrame.size.height) animated:NO];
[m_pPopOverController presentPopoverFromRect:CGRectMake(theButton.frame.origin.x,0,40,40) inView:self
permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
OptionViewController is a UIViewController which will contain UITableView.Populate UITableView with data(list of options).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([m_Target respondsToSelector:m_Selector]) {
[m_Target performSelector:m_Selector withObject:nil];
}
}
Do not forget to set target by calling this method, so when user selects any option, corresponding method in mainviewcontroller is called where you want selected value.
- (void)setTarget:(id)inTarget andSelector:(SEL)inSelector
{
m_Target = inTarget;
m_Selector = inSelector;
}

Related

MBProgressHUD activity indicator doesn't show in viewDidAppear after segue is performed

I have two UITableViewControllers A and B, and this is what I'm trying to do when I click on a table view cell in A:
Prepare to segue from A to B by setting some of B's variables from A.
Perform segue from A to B.
B appears.
Display a "Loading" activity indicator with [MBProgressHUD][1].
In a background task, retrieve data from a URL.
If an error occurs in the URL request (either no data received or non-200 status code), (a) hide activity indicator, then (b) display UIAlertView with an error message
Else, (a) Reload B's tableView with the retrieved data, then (b) Hide activity indicator
However, this is what's happening, and I don't know how to fix it:
After clicking a cell in A, B slides in from the right with an empty plain UITableView. The MBProgressHUD DOES NOT SHOW.
After a while, the tableView reloads with the retrieved data, with the MBProgressHUD appearing very briefly.
The MBProgressHUD immediately disappears.
There doesn't seem to be an error with the way the background task is performed. My problem is, how do I display the MBProgressHUD activity indicator as soon as my B view controller appears? (And actually, how come it's not showing?) Code is below.
A's prepareForSegue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
B *b = (B *)[segue destinationViewController];
// Set some of B's variables here...
}
Relevant methods in B
- (void)viewDidAppear:(BOOL)animated {
[self startOver];
}
- (void)startOver {
[self displayLoadingAndDisableTableViewInteractions];
[self retrieveListings];
[self.tableView reloadData];
[self hideLoadingAndEnableTableViewInteractions];
}
- (void)displayLoadingAndDisableTableViewInteractions {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = #"Loading";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.tableView.userInteractionEnabled = NO;
}
- (void)hideLoadingAndEnableTableViewInteractions {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.tableView.userInteractionEnabled = YES;
}
- (void)retrieveListings {
__block NSArray *newSearchResults;
// Perform synchronous URL request in another thread.
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
newSearchResults = [self fetchNewSearchResults];
});
// If nil was returned, there must have been some error--display a UIAlertView.
if (newSearchResults == nil) {
[[[UIAlertView alloc] initWithTitle:#"Oops!" message:#"An unknown error occurred. Try again later?" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
} else {
// Add the retrieved data to this UITableView's model. Then,
[self.tableView reloadData];
}
}
- (NSArray *)fetchNewSearchResults {
// Assemble NSMutableArray called newSearchResults from NSURLConnection data.
// Return nil if an error or a non-200 response code occurred.
return newSearchResults;
}
I think you have to call [self hideLoadingAndEnableTableViewInteractions]; after newSearchResults = [self fetchNewSearchResults]; You are retrieving data in another thread which means -startOver will continue executing after calling [self retrieveListings]; and will hide the HUD right away. Also because you are updating the display you have to make sure you are doing that on the main thread. See example
dispatch_async(dispatch_get_main_queue(), ^{
//update UI here
});
When B appears, it displays a plain and empty UITableView, but does not display the MBProgressHUD even if the task does begin in the background (and yet, the MBProgressHUD is called to show before that). Hence, my solution is to show the MBProgressHUD in viewDidLoad, which precedes viewWillAppear.
- (void)viewDidLoad {
// ...
[self displayLoadingAndDisableUI];
}
I set up two additional boolean properties to B--one in .h, called shouldStartOverUponAppearing, and one in a class extension in .m, called isLoadingAndDisabledUI. In startOver, I added the following lines:
- (void)startOver {
if (!self.isLoadingAndDisabledUI) {
[self displayLoadingAndDisabledUI];
}
}
The check is done so that startOver doesn't display another MBProgressHUD when it has already been displayed from viewDidLoad. That is because I have a third view controller, called C, that may call on B's startOver, but doesn't need to call viewDidLoad just to display the MBProgressHUD.
Also, this is how I defined viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
if (self.shouldStartOverUponAppearing) {
[self startOver];
self.shouldStartOverUponAppearing = NO;
}
}
This way, startOver will only be invoked IF B appeared from A. If B appears by pressing "Back" in C, it will do nothing and only display the old data that was there.
I think that this solution is FAR from elegant, but it works. I guess I'll just ask for a better approach in a separate SO question.
I have used a common method for MBProgressHUD.
#import "MBProgressHUD.h" in AppDelegate.h also following methods.
- (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title;
- (void)dismissGlobalHUD;
In AppDelegate.m add following methods.
- (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
[MBProgressHUD hideAllHUDsForView:self.window animated:YES];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
hud.labelText = title;
return hud;
}
- (void)dismissGlobalHUD {
[MBProgressHUD hideHUDForView:self.window animated:YES];
}
How to use?
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication]delegate];
//Show Indicator
[appDel showGlobalProgressHUDWithTitle:#"Loading..."];
//Hide Indicator
[appDel dismissGlobalHUD];
Hope this helps.

How to invalidate timer while changing tab in tab bar

When I am moving from one tab to another in the timer continues to run.
But I want to stop that timer.
So if anyone knows then please tell me which method i need to call while moving from one tab to another.
simplest answer is
-(void)viewWillDisappear:(BOOL)animated {
//here
}
when ever you will click on tab, this function will be called and everything written in this function will be executed
You can stop timer process like this,
if(self.TimeOfActiveUser)
{
[self.TimeOfActiveUser invalidate];
self.TimeOfActiveUser = nil;
}
Edit
You can put this method when you pushing another method For Example: if you using Timer for Reload Data of TableView every 6 second then you can push particular row by selecting then you can put this two line of code in
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.TimeOfActiveUser invalidate];
self.TimeOfActiveUser = nil;
objScore = [[CntrScore alloc]initWithNibName:#"CntrScore" bundle:nil];
[self.navigationController pushViewController:objScore animated:YES];
}
AND
if your app Tab based you put timer stop code in viewWillDisappear
-(void)viewWillDisappear:(BOOL)animated
{
if(self.TimeOfActiveUser)
{
[self.TimeOfActiveUser invalidate];
self.TimeOfActiveUser = nil;
}
}
Check the tab bar index and write the code which is given below:
[yourtimername invalidate];

editingStyleForRowAtIndexPath -- fires in iOS 5, does not fire in iOS 6

I have simple master and detail view controllers connected with two segues, one for "show detail" and one for "add new".
"Show detail" segues to the detail view controller with setEditing:NO.
Tap "+" (add icon) segues to the detail view controller with setEditing:YES
iOS 5.1: "+" works as I expect, the detail page is in edit mode and editingStyleForRowAtIndexPath fires to show the insert and delete indicators.
iOS 6.0: the "+" makes the transition to the detail page but editingStyleForRowAtIndexPath never fires. Other code that is in setEditing:YES gets executed. didSelectRowAtIndexPath does fire (delegate = self).
Once on the detail page edit mode works as expected in both cases.
Any ideas?
// Master.m
if([[segue identifier] isEqualToString:#"NewRecipe"]) {
DetailViewController *detailViewController = [segue destinationViewController];
// stuff
detailViewController.recipe = r;
detailViewController.delegate = self;
detailViewController.editing = YES;
}
// Detail.m
-(void)setEditing:(BOOL)flag animated:(BOOL)animated {
if (flag) {
[self.tableView setEditing:flag animated:YES];
[self.tableView beginUpdates];
// the row does get added
[self.tableView insertRowsAtIndexPaths:#[pathToAdd] withRowAnimation:UITableViewRowAnimationAutomatic];
// datasource gets updated here
[self.tableView endUpdates];
....
}
}
I figured it out. I don't know why this is the fix, I hope this does not replace bad code with worse code.
Master.m
// iOS 5 -- this is OK
detailViewController.editing = YES;
For iOS 6 I needed the detailViewController to make a call to a delegate method to determine whether to setEditing:YES.
Master.m
-(BOOL)isNewRecipe {
return (_isNewRecipe == 1);
}
Detail.m
if ([self.delegate isNewRecipe]) {
[self setEditing:YES];
}

Three20 Show Menu For Cell Without Sliding Animation

[self showMenu:view forCell:cell animated:NO];
How do you override this method in Three20 so that it won't do a sliding animation and it won't remove the contents of your TableViewCell?
I just want to show menu like what Facebook App does when showing the Comment and Like Menu
Well if ever, there's still someone out there having this problem, what I did was to override both of these methods, and somehow, it's working fine..
- (void)showMenu:(UIView *)view forCell:(UITableViewCell *)cell animated:(BOOL)animated
{
[self hideMenu:NO];
_menuView = [view retain];
_menuCell = [cell retain];
[_menuCell.contentView addSubview:_menuView];
}
- (void)hideMenu:(BOOL)animated {
if (_menuView) {
[_menuView removeFromSuperview];
TT_RELEASE_SAFELY(_menuView);
TT_RELEASE_SAFELY(_menuCell);
}
}

Question on edit button on sqlite

I am trying to show minus symbol when I show data from an sqlite database, but if we show an edit button it shows a delete symbol. I am using navigation controller to show data. I want to show when the page loads, it must be with a "-" symbol without edit button? Does anyone have an example or tutorial?
If I understand you correctly, you want to delete the row immediately, without confirmation of tapping a delete button.
First: Users are used to having a delete button confirmation, and if you delete immediately, they may be surprised.
Second: If the issue is deleting many rows quickly, could you instead allow the user to select multiple rows and delete them all at once (like in the email app)
Third: A solution to your question:
Make a custom UITableViewCell and override this function
- (void)willTransitionToState:(UITableViewCellStateMask)state {
if (state && UITableViewCellStateShowingDeleteConfirmationMask) {
[delegate deleteCell:self];
}
}
You will need to also have a delegate property on the UITableViewCell subclass and set it to your view controller whenever you create a new cell. Then add this function in your view controller:
- (void) deleteCell:(UITableViewCell *)cell {
NSIndexPath *idx = [self.table indexPathForCell:cell];
if (idx) {
NSArray *arr = [NSArray arrayWithObject:idx];
//Delete from database here
if (/*delete success*/) {
[self.tableView deleteRowsAtIndexPaths:arr withRowAnimation:UITableViewRowAnimationFade];
}
}
}
Still not sure exactly what you're looking for, but if I understand better this time, you just want the - symbol to show up as soon as the view appears?
- (void) viewDidLoad {
[super viewDidLoad];
[self.tableView setEditing:YES];
}