IBAction from different class not functioning? - iphone

I have a file called 'LoginViewController' that needs to do an action from the 'RootViewController.m' file. I did a NSLog and it functions, but the actual script doesn't.
I have also made sure to #import "RootViewController.h" in my LoginViewController.
LoginViewController
RootViewController *test = [[RootViewController alloc] init];
[NSThread detachNewThreadSelector:#selector(updateAlbumsAfterLogin) toTarget:test withObject:nil];
RootViewController
- (void)updateAlbumsAfterLogin {
NSLog(#"Updating albums after login!");
// Set up loading box and show it
UIImage *imageLoop = [UIImage imageNamed:#"loading_box.png"];
imagefour = [[UIImageView alloc] initWithImage:imageLoop];
imagefour.frame = CGRectMake(100, 80, 116, 116);
[self.view addSubview:imagefour];
// Set up loading text and show it
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(97, 60, 130, 100)];
myLabel.text = #"Loading...";
myLabel.textColor = [UIColor whiteColor];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.font = [UIFont fontWithName:#"Helvetica" size: 16.0];
myLabel.numberOfLines = 0;
//[myLabel sizeToFit];
[self.view addSubview:myLabel];
// Set up spinner and show it
myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
myIndicator.center = CGPointMake(158, 155);
myIndicator.hidesWhenStopped = YES;
[self.view addSubview:myIndicator];
[myIndicator startAnimating];
[NSThread detachNewThreadSelector:#selector(updateAlbums) toTarget:self withObject:nil];
//myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector: #selector(updateAlbums) userInfo: nil repeats: YES];
}
Thanks in advance,
Coulton.
If you need any other code, feel free to ask for it!

You are detaching a new thread to do this work and update your view, but only the main thread is ever allowed to update the view. That's why you can see your NSLog() messages happening but nothing in your UI updates.
The standard method to handle this is for your main thread to detach a new thread (or perform selector on a background thread), then once the fetching and data handling is complete you perform selector on the main thread which uses the data you fetched to update the UI.

Related

Preload circle for UITableView

I search for a lot forums but nothing found, my problem is that
I need preload some data from the web to load to the UITableView.
I saw some apps solved this problem, when show UITableView they shows "waiting circle" as modal view and after data is loaded just hide this circle.
Is there simple solution to do this?
What I need to do?
What kind of Request do I need: synchronius or asynchronius?
How to show this circle, do I need to show animatedGif or there is some internal/external control for this?
You need to make a NSThread for your "waiting circle"-(Activity Indicator).
1)Threads How do I create an NSThread that isn't the main thread.
2)Activity Indicator How to use activity indicator view.
- (void)showWaitingView {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGRect frame = CGRectMake(90, 190, 32, 32);
UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[progressInd startAnimating];
progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
frame = CGRectMake(130, 193, 140, 30);
UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
waitingLable.text = #"Processing...";
waitingLable.textColor = [UIColor whiteColor];
waitingLable.font = [UIFont systemFontOfSize:20];;
waitingLable.backgroundColor = [UIColor clearColor];
frame = [[UIScreen mainScreen] applicationFrame];
UIView *theView = [[UIView alloc] initWithFrame:frame];
theView.backgroundColor = [UIColor blackColor];
theView.alpha = 0.7;
theView.tag = 999;
[theView addSubview:progressInd];
[theView addSubview:waitingLable];
[progressInd release];
[waitingLable release];
[window addSubview:[theView autorelease]];
[window bringSubviewToFront:theView];
[pool drain];
}
- (void)removeWaitingView {
UIView *v = [window viewWithTag:999];
if(v) [v removeFromSuperview];
}

iPhone viewForHeaderInSection

I have an application with navigation controller and some table view controller. In table view controller I have a two section header my definitions:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0) {
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 74)];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"toolbarTopBack.png"]];
UILabel *headline = [[UILabel alloc] initWithFrame:CGRectMake(11, 14, 305, 21)];
headline.backgroundColor = [UIColor clearColor];
headline.textColor = [UIColor whiteColor];
headline.font = [UIFont fontWithName:#"Helvetica Neue" size:21];
headline.text = searchPosition;
UILabel *subHeadline = [[UILabel alloc] initWithFrame:CGRectMake(11, 36, 305, 21)];
subHeadline.backgroundColor = [UIColor clearColor];
subHeadline.textColor = [UIColor grayColor];
subHeadline.font = [UIFont fontWithName:#"Helvetica Neue" size:16];
subHeadline.text = searchRegion;
[customView addSubview:myImageView];
[customView addSubview:headline];
[customView addSubview:subHeadline];
return customView;
} else {
// create the parent view that will hold header Label
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
customView.userInteractionEnabled = YES;
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"mainToolBar.png"]];
UIToolbar *topToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
topToolbar.barStyle = UIBarStyleDefault;
[topToolbar setBackgroundColor:[UIColor clearColor]];
static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{
NSMutableDictionary *appSettingsData = [[NSMutableDictionary alloc] initWithDictionary:[appDelegate getAppStrings]];
NSArray *segmentItems = [NSArray arrayWithObjects:[NSString stringWithFormat:#"%# (%d)", [appSettingsData valueForKey:#"segmentButton4"], listCountOffers], [NSString stringWithFormat:#"%# (%d)", [appSettingsData valueForKey:#"segmentButton5"], [[appDelegate comunication] getSimilarCount:[appDelegate getCurrentCI] idPosition:idPosition idRegion:idRegion]], nil];
segmentControl = [[UISegmentedControl alloc] initWithItems:segmentItems];
segmentControl.frame = CGRectMake(6, 8, 308, 29);
segmentControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentControl setTintColor:[UIColor grayColor]];
[segmentControl addTarget:self action:#selector(segmentedControlIndexChanged:) forControlEvents:UIControlEventValueChanged];
segmentControl.momentary = NO;
segmentControl.selectedSegmentIndex = 0;
});
UIBarButtonItem *toolbarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentControl];
[topToolbar addSubview:toolbarItem.customView];
[customView addSubview:myImageView];
[customView addSubview:topToolbar];
return customView;
}
}
I use "static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{", because I need create this header only first time (because when I scroll table, method is call and call ... and this is wrong) ...
Everything works fine, when I create table view controller it show a headers, when I scroll it, nothing is recreating (it is fine), but when I push back button a then reopen tableview controller headers are empty. Where is problem ? Is there any solution, how to fix it ? Thanks a lot
Well, to me it looks like it's a problem with your static dispatch.
When you push the Back button, chances are that your view holding the table view is released (I don't know your code, but I suppose it is like that), meaning all internal variables are gone - except your static dispatch, which won't be called the next time you instantiate the view. So, during the next instantiation your segmentItems will not be created, but because the view was released, they are empty. You should solve your 'only create once' problem differently, e.g. by remembering the created segmentItems in a dictionary and getting them from there if they do not exist yet.
You're using dispatch_once without really needing to. The block will only be executed once, even if you subsequently remove the view controller from memory and deallocate segmentControl.
Use lazy loading instead - create a property for your segmentControl view within your view controller, and in the accessor for that, if the backing ivar is nil, create it then:
Your synthesize statement:
#synthesize segmentControl = _segmentControl
Your accessor method:
-(UISegmentedControl*)segmentControl
{
if (_segmentControl)
return _segmentControl;
UISegmentedControl *segmentControl = //... create your control here
self.segmentControl = segmentControl
return segmentControl;
}
Then when you want to use the view, use self.segmentControl. The first time you call it, it will be created, the subsequent times, it will be re-used.
check it with breakpoint and see the process. it maybe the memory allocation problem. See this it may help you. http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/

problem i loading progress indicator in iphone sdk?

I want to get list from server it is taking some time So at that time I'm displaying progress indicator. My problem sometimes the progress indicator is appearing and some times it is not appearing. The code I used is
-(void)viewWillAppear:(BOOL)animated
{
[self loadprogressIndicator];
}
-(void)loadprogressIndicator
{
MessengerAppDelegate* appDelegate = (MessengerAppDelegate*) [ [UIApplication sharedApplication] delegate];
[appDelegate showWaitingView];
[NSThread detachNewThreadSelector:#selector(statusIndicatorForRefresh) toTarget:self withObject:nil];
}
-(void)statusIndicatorForRefresh
{
NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self performSelectorOnMainThread:#selector(loadList) withObject:nil waitUntilDone:NO];
[NSThread exit];
[pool release];
}
-(void)loadList
{
MessengerAppDelegate* appDelegate = (MessengerAppDelegate*) [ [UIApplication sharedApplication] delegate];
customerList = [appDelegate getCustomersArray];
[theTableView reloadData];
[appDelegate removeWaitingView];
}
And in appdelegate.m I implemeted these two methods
- (void)showWaitingView
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGRect frame = CGRectMake(90, 190, 32, 32);
UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc] initWithFrame:frame];
[progressInd startAnimating];
progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
frame = CGRectMake(130, 193, 140, 30);
UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];
waitingLable.text = #"Processing...";
waitingLable.textColor = [UIColor whiteColor];
waitingLable.font = [UIFont systemFontOfSize:20];
waitingLable.backgroundColor = [UIColor clearColor];
frame = [[UIScreen mainScreen] applicationFrame];
UIView *theView = [[UIView alloc] initWithFrame:frame];
theView.backgroundColor = [UIColor blackColor];
theView.alpha = 0.7;
theView.tag = 999;
[theView addSubview:progressInd];
[theView addSubview:waitingLable];
[progressInd release];
[waitingLable release];
[window addSubview:[theView autorelease]];
[window bringSubviewToFront:theView];
[pool drain];
}
- (void)removeWaitingView
{
UIView *v = [window viewWithTag:999];
if(v) [v removeFromSuperview];
}
Can anyone please help me. Happy Coding..
Thanks in advance.
Praveena..
You are doing all your activity on the main thread. Your app will appear to be blocked because you block the UI thread.
Also what is the use of launching an NSThread if all it does is call a method on your main thread and not even wait for it to complete? You are doing no multithreading at all.
Your app does this:
in main thread:
show waiting view
do 1 run loop cycle
do your data processing
hide waiting view
since your data processing is in the main thread you block everything there. You should do the processing in a secondary thread and leave the main thread for UI handling.

iPhone: Add "loading" subView

I am wanting to show a simple loading dialog when certain things are happening in my app. I figured I would just create a new view, add a label to that, and then set that view to a subView of my current view.
When doing this, I don't see anything!
Here is how I am writing my method:
- (void)showLoading {
UIView *loading = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
loading.backgroundColor = [UIColor blackColor];
UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(198, 9, 94, 27)];
txt.text = #"Loading...";
txt.textColor = [UIColor whiteColor];
[loading addSubview:txt];
[super.view addSubview:loading];
[super.view bringSubviewToFront:loading];
[loading release];
[txt release];
}
Am I doing this completely wrong?
EDIT:
I added it to the viewDidLoad method, and it works how I want:
loading = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
loading.backgroundColor = [UIColor blackColor];
UILabel *txt = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 94, 27)];
txt.text = #"Loading...";
txt.textColor = [UIColor whiteColor];
[loading addSubview:txt];
[txt release];
[self.view addSubview:loading];
[self.view bringSubviewToFront:loading];
But when loading it from a method, it seems to lag, and not show up for a bit.
Although this doesn't directly answer your question, I'd recommend grabbing MBProgressHUD from GitHub and using that in place of a static label. Looks better, less code for you to directly maintain, etc. You can find it at http://github.com/matej/MBProgressHUD
The way I use it is by creating a subclass of UITableViewController and defining a handful of methods to show and hide the HUD view. From there, I call each relevant method when I'm loading or done loading.
Specifically, I have four methods: -hudView, -showLoadingUI, -showLoadingUIWithText:, and -hideLoadingUI.
-hudView creates a new MBProgressHUD object if one doesn't already exist, and adds it to the current view ([self.view addSubview:hudView]).
-showLoadingUI calls -showLoadingUIWithText: with a default title, -showLoadingUIWithText: just unhides the MBProgressHUD and sets a label value for it (self.hudView.labelText = #"foo";).
-hideLoadingUI hides the hudView ([self.hudView hide:YES]).
First, I don't think UIView has method called init. You may just call the super of it. The appropriate method you should call is - (id)initWithFrame:(CGRect)aRect . The frame is the position, the size of the View you want to display. More here
Another thing is why you call [super.view addSubView:], I think it should be self.view, isn't it?

Custom back button click event on pushed view controller

I have pushed view controller and load WebView and Custom rectangular rounded button on right down left corner into view using programmatic way.
-(void)loadView {
CGRect frame = CGRectMake(0.0, 0.0, 480, 320);
WebView = [[[UIWebView alloc] initWithFrame:frame] autorelease];
WebView.backgroundColor = [UIColor whiteColor];
WebView.scalesPageToFit = YES;
WebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin);
WebView.autoresizesSubviews = YES;
WebView.exclusiveTouch = YES;
WebView.clearsContextBeforeDrawing = YES;
self.roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
self.roundedButtonType.frame = CGRectMake(416.0, 270.0, 44, 19);
[self.roundedButtonType setTitle:#"Back" forState:UIControlStateNormal];
self.roundedButtonType.backgroundColor = [UIColor grayColor];
[self.roundedButtonType addTarget:self action:#selector(back:) forControlEvents:UIControlEventTouchUpInside];
self.view = WebView;
[self.view addSubview: self.roundedButtonType ];
[WebView release];
}
This is action that I have added as back button of navigation.
-(void)back:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
-(void)viewDidUnload{
self.WebView = nil;
self.roundedButtonType = nil;
}
-(void)dealloc{
[roundedButtonType release];
[super dealloc];
}
Here, When Back button click then it is showing previous view but application got stuck in that view and GDB shows Program received signal :EXC_BAD_ACCESS message.
how resolve this issue?
Thanks,
You have -autorelease'd WebView here:
WebView = [[[UIWebView alloc] initWithFrame:frame] autorelease];
but then you -release it again!
[self.view addSubview: self.roundedButtonType ];
[WebView release];
Try to remove one of the releases.
Also,
self.roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
unless roundedButtonType is declared as #property(assign,...), you don't need to send the -retain message. And it's better to setup the frame, title, etc. before assigning to self.roundedButtonType, because every call to self.roundedButtonType is not free.