subsequent instances of a UIView subclass causing display error? - iphone

I made a Stock Tiker to display continuous stock objects. And Working fine for first Instance.
The implementation of the ticket code is as follows:
- (void)viewDidLoad
{
tickerView=[[StockTiker alloc] init];
[tickerView setFrame:CGRectMake(0, 0, 320, 20)];
tickerView.delegate=self;
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 20)];
[label setBackgroundColor:[UIColor redColor]];
label.text=#"First Object";
UILabel *label2=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
[label2 setBackgroundColor:[UIColor grayColor]];
label2.text=#"Second Object";
UILabel *label3=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 20)];
[label3 setBackgroundColor:[UIColor magentaColor]];
label3.text=#"Third Object";
UILabel *label4=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 20)];
[label4 setBackgroundColor:[UIColor yellowColor]];
label4.text=#"Fourth Object";
UILabel *label5=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 20)];
[label5 setBackgroundColor:[UIColor cyanColor]];
label5.text=#"Fifth Object";
viewArray=[[NSArray alloc] initWithObjects:label,label2,label3,label4,label5,nil];
[self.view addSubview:tickerView];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark- Ticker Delegate..
-(UIView*)viewForRow:(int)row inTicker:(StockTiker*)stock_Ticker
{
return [viewArray objectAtIndex:row];
}
-(int)numberOfRowsForStockTiker:(StockTiker*)stock_Ticker
{
return [viewArray count];
}
//Output is fine
But when I made second Instance of Ticker class it overlapped to each other.
Output with two instance managed using ticker.tag
Any Idea?
How can I solve this error.
Thanks in Advance!
Hey I have uploaded an example please check it Horizontal List

Added objectArray2.
Duplicated BSE_sticker method
in second_Sticker did all the same code of BSE_Sticker except add it to objectArray2.
Return objectARray2 for stkticker2 in delegate based on tag
Project here
Edit
I looked into issue and solved repeating instance/ wobbling
Your ticker class uses static int count.
Static variables are instantiated only once per class. and therefore
you coding regarding counter leads the object to check for instance
multiple times.
you should change the static variable to a normal ivar and instantiate it to 0 in start method.
Then it will work fine

Related

Adding a subview to another subview is not working

I am trying to add label (headerLabel) to another subview (introPadTutorial) here and it is not showing up on the screen. Can some please help me where am I making mistake?
//Header file
#property (nonatomic, retain) UIView *introPadTutorial;
//Implementation file
#synthesize introPadTutorial;
UIView *v = [_appDelegate getCurrentView];
CGRect tutorialFrame = [self getTableViewFrame];
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
[introPadTutorial setBackgroundColor:[UIColor uberLightGray]];
[introPadTutorial setUserInteractionEnabled:YES];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 180, 180)];
[headerLabel setText:#"test test test"];
[headerLabel setBackgroundColor:[UIColor greenColor]];
[headerLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
[headerLabel setTextColor:[UIColor redColor]];
[headerLabel setShadowColor:[UIColor whiteColor]];
[headerLabel setShadowOffset:CGSizeMake(0, 1)];
[self.introPadTutorial addSubview:headerLabel];
[headerLabel release];
[v addSubview:introPadTutorial];
[introPadTutorial release];
Don't use a negative width to change the POSITION, use the origin to change the position.
Use this:
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width - tutorialFrame.size.width, 80, tutorialFrame.size.width, v.frame.size.height)];
I think that should help make the subview actually show up.
May be your...
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(v.frame.size.width, 80, -tutorialFrame.size.width, v.frame.size.height)];
should actually be...
introPadTutorial = [[UIView alloc] initWithFrame:CGRectMake(0, 80, tutorialFrame.size.width, v.frame.size.height)];
Is your introPadTutorial view showing up? You have a negative width in it's initWithFrame call.

Programmatically add UILabel to toolbar

I am trying to add a UILabel programmatically into my UIToolBar but it dose not seem to be appearing. This is what I am doing with my code.
- (void) viewWillAppear:(BOOL)animated
{
// Create custom toolbar at top of screen under navigation controller
[matchingSeriesInfoToolBar setFrame:CGRectMake(0, 60, 320, 30)];
matchingSeriesInfoToolBar = [UIToolbar new];
[matchingSeriesInfoToolBar sizeToFit];
CGFloat toolbarHeight = 30;
CGRect mainViewBounds = [[UIScreen mainScreen] applicationFrame];
[matchingSeriesInfoToolBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds), 0, CGRectGetWidth(mainViewBounds), toolbarHeight)];
matchingSeriesInfoToolBar.tintColor = [UIColor darkGrayColor];
[self.view addSubview:matchingSeriesInfoToolBar];
// Create size of uitableview (to fit toolbar.
[matchingSeriesTableView setFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 30)];
[self.view addSubview:matchingSeriesTableView];
// Ad UILabel to the toolbar
UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:manufSelectionLabel];
matchingSeriesInfoToolBar.items = [NSArray arrayWithObject:textFieldItem];
manufSelectionLabel.text = #"Hello World!";
[super viewWillAppear:animated];
}
So pretty much I have created a custom toolbar which I have changed the usual location from the bottom of the screen, to appear under the UINavigationController, this is also added to the view like this so it animated properly in the view transitions..
After which I create the size of the tableview so that it appears after the custom toolbar..
then from there I am trying to add a UILabel to the toolbar.. but for some reason its not working out.
Any help would be greatly appreciated.
You must actually create the label somewhere. This code works just fine.
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:toolbar];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
label.backgroundColor = [UIColor clearColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:label];
toolbar.items = [NSArray arrayWithObject:item];
label.text = #"Hello World";
In the code you posted you don't ever make a UILabel. Your comment says Ad UILabel to the toolbar but you then proceed to make a UIBarButtonItem with a custom view manufSectionLabel.
Where is the code which creates manufSectionLabel?
PS This line does nothing :
[matchingSeriesInfoToolBar setFrame:CGRectMake(0, 60, 320, 30)];
because at that point matchingSeriesInfoToolbar is nil - you haven't made it yet!

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/

objective c addSubview order

i am a junior programmer
I face a problem of some of the subview in the program not shown ,
don't know if it is the addsubview order problem or the others .
hope if anyone can provide a suggestion or solution .thanks all
for (int i = 0; i < NUM_DISHES; i++) {
UIImageView* img_steps = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg", i]]];
//IMPORTANT: The i*W_IPAD means that 1st 0*1024 , 1*1024 , 2*1024.............and so on
[img_steps setFrame:CGRectMake(W_IPAD, 0, W_IPAD , H_UPFR)];
UIImageView* imageset = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[ary_img objectAtIndex:i]]];
[imageset setFrame:CGRectMake(89, 9, H_INGREPHOTO, W_INGREPHOTO)];
[ary_image addObject:imageset];
//ingredient amount section
UILabel* lbl_amt = [[UILabel alloc] initWithFrame:CGRectMake(128, 177, 190, 33)];
[lbl_amt setText:[ary_amount objectAtIndex:i]];
[lbl_amt setFont:[UIFont systemFontOfSize:25]];
[lbl_amt setTextColor:[UIColor blackColor]];
[lbl_amt setBackgroundColor:[UIColor clearColor]];
[ary_amt addObject:lbl_amt];
//method section
UILabel* lbl_method = [[UILabel alloc] initWithFrame:CGRectMake(596, 93, 130, 32)];
[lbl_method setText:[ary_meth objectAtIndex:i]];
[lbl_method setBackgroundColor:[UIColor clearColor]];
[lbl_method setFont:[UIFont systemFontOfSize:30]];
[lbl_method setTextColor:[UIColor blackColor]];
[ary_method addObject:lbl_method];
//alpha bar step number step number section
UILabel* lbl_numberstep = [[UILabel alloc] initWithFrame:CGRectMake(90 + (130 * i), 5, W_NUMBERSTEP, H_NUMBERSTEP)];
[lbl_numberstep setText:[NSString stringWithFormat:#"%d",i + 1]];
[lbl_numberstep setFont:[UIFont systemFontOfSize:20]];
[lbl_numberstep setBackgroundColor:[UIColor clearColor]];
[lbl_numberstep setTextColor:[UIColor blackColor]];
[self.view addSubview:img_steps];
[alpha_bar addSubview:lbl_numberstep];
[alphaframe addSubview:[ary_method objectAtIndex:i]];
[alphaframe addSubview:[ary_amt objectAtIndex:i]];
[alphaframe addSubview:[ary_image objectAtIndex:i]];
}
// Section --- Add Subview
[self.view addSubview:background];
[background addSubview:main_view];
[main_view addSubview:alpha_bar];
[main_view addSubview:alphaframe];
[main_view addSubview:but_transit_to_a]; //can add to the main_view
[main_view addSubview:left_buttom];
[main_view addSubview:right_buttom];
[alpha_bar addSubview:bar];
[alpha_bar addSubview:lbl_lastnum];
[alphaframe addSubview:but_transit_to_c];
}
return self;
}
To bring the subview to front you can use [yourMainView bringSubviewToFront:yoursubview]. If your subview isn't showing then, check that you've properly set up the subview's frame and that hidden = NO.
The view.subviews array holds the bottom-most view in index zero, and holds views in an order relating to their "z-index". So [myView.subviews lastObject]; will always give the view in front of all other views and [myView.subviews objectAtIndex:0]; will have the view below all views in that array.
From the UIView docs - subviews property:
You can use this property to retrieve the subviews associated with
your custom view hierarchies. The order of the subviews in the array
reflects their visible order on the screen, with the view at index 0
being the backmost view. For complex views declared in UIKit and other
system frameworks, any subviews of the view are generally considered
private and subject to change at any time. Therefore, you should not
attempt to retrieve or modify subviews for these types of
system-supplied views. If you do, your code may break during a future
system update.

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?