I am trying to add Custom UIViews ( Graphs drawn using Core Plot ) as the elements in iCarousel, but I dont see the Graphs being drawn. Here is my header file :
#interface MTViewController : UIViewController <iCarouselDataSource, iCarouselDelegate,
CPTBarPlotDataSource, CPTBarPlotDelegate>
#property (strong, nonatomic) IBOutlet iCarousel *aCarousel;
#property (strong, nonatomic) IBOutlet UILabel *label;
#property (nonatomic) BOOL wrap;
#property (strong, nonatomic) IBOutlet CPTGraphHostingView *graphView;
#property (nonatomic, retain) NSMutableArray *data;
#property (strong, nonatomic) NSMutableArray *animals;
#property (nonatomic, retain) CPTXYGraph *graph;
#property (strong, nonatomic) NSMutableArray *graphs;
#property (strong, nonatomic) NSMutableArray *descriptions;
#end
Implementation file is :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//set up carousel data
wrap = NO;
self.graphs = [NSMutableArray arrayWithObjects: self.graphView,nil];
}
return self;
}
- (void)viewDidLoad
{
aCarousel.type = iCarouselTypeCoverFlow2;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setACarousel:nil];
[self setLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#pragma - mark iCarousel Delegate
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [self.graphs count];
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
// limit the number of item views loaded concurrently (for performance)
return 1;
}
- (CPTGraphHostingView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(CPTGraphHostingView *)view
{
// create a numbered view
// view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[animals objectAtIndex:index]]];
// return view;
CGSize size = self.graphView.frame.size;
float width = size.width;
CPTGraphHostingView *currentGraph ;
for (int i = 0; i < self.graphs.count; i++) {
CGRect frame;
frame.origin.x = width * i;
frame.origin.y = 0;
frame.size = size;
currentGraph = [graphs objectAtIndex:i];
currentGraph.frame = frame;
[self generateBarPlot:frame];
}
return currentGraph;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
return 1;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
// usually this should be slightly wider than the item views
return 240;
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
return self.wrap;
}
- (void)carouselDidScroll:(iCarousel *)carousel
{
//[label setText:[NSString stringWithFormat:#"%#", [descriptions objectAtIndex:carousel.currentItemIndex]]];
[label setText:[NSString stringWithFormat:#"Testing"]];
}
.... followed by methods to generate graph
Any help will be deeply appreciated.
Thanks for reading.
I solved it by casting the Custom View to UIView.
Related
I want to put some static content (buttons, views etc) on UIPageViewController. But I don't know how.
Any ideas?
Just create a container uiviewcontroller that will have e.g. a static button and an empty uiview. Create them as iboutlets and a usual property UIPageViewController called pvc. Then you can create a pvc in code and add it to the uiview outlet via addSubview:.
This is some code copied from a project i did last weeek. There are Skidata in a UIPageViewController and put an UIImage of a snowflake as overlay about it. I copied only the relevant parts of code. Hope this will help you:
SkiViewController.m
#interface SkiViewController () <UIPageViewControllerDataSource>
#property (strong, nonatomic) UIPageViewController *pageController;
#property (strong, nonatomic) SkiDataArray* skiDataArray;
#property (weak, nonatomic) IBOutlet UIImageView *imageView;
#property (weak, nonatomic) IBOutlet UIView *pageDataView;
#end
#implementation SkiViewController
- (SkiSubViewController *)viewControllerAtIndex:(NSUInteger)index
{
SkiSubViewController *childViewController = [[SkiSubViewController alloc] initWithNibName:#"SkiSubViewController" bundle:nil];
childViewController.indexNumber = index;
if(self.skiDataArray)
{
childViewController.skiData = self.skiDataArray[index];
}
return childViewController;
}
#pragma mark - lify cycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.pageController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
self.pageController.dataSource = self;
[self addPageControllerViewControllers];
[self addPageControllerView];
[self addChildViewController:self.pageController];
[self.pageController didMoveToParentViewController:self];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self updateData]; // download ski data and stuff
}
- (void)addPageControllerViewControllers
{
SkiSubViewController *viewController = [self viewControllerAtIndex:0];
if(self.skiDataArray)
{
viewController.skiData = self.skiDataArray[0];
}
[self.pageController setViewControllers:#[viewController] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
}
- (void)addPageControllerView
{
self.pageController.view.translatesAutoresizingMaskIntoConstraints = NO; // avoids conflicts with auto generated constraints
[self.pageDataView addSubview:self.pageController.view];
NSDictionary *views = #{ #"subview": self.pageController.view };
[self.pageDataView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[subview]|" options:0 metrics: 0 views:views]];
[self.pageDataView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[subview]|" options:0 metrics: 0 views:views]];
[self.pageDataView updateConstraintsIfNeeded];
}
#pragma mark - uipageviewcontroller data source
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [(SkiSubViewController *)viewController indexNumber];
if (index == 0)
{
return nil;
}
else
{
index--;
return [self viewControllerAtIndex:index];
}
}
- (UIViewController*)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = ((SkiSubViewController *)viewController).indexNumber;
index++;
if (index == self.skiDataArray.count)
{
return nil;
}
else
{
return [self viewControllerAtIndex:index];
}
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return self.skiDataArray.count;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
#end
SkiSubViewController.h
#interface SkiSubViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (assign, nonatomic) NSInteger indexNumber;
#property (strong, nonatomic) SkiData* skiData;
#end
SkiSubViewController.m
#interface SkiSubViewController ()
#property (weak, nonatomic) IBOutlet UILabel *label;
#end
#implementation SkiSubViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.label.text = [NSString stringWithFormat:#"Number %li", (long)self.indexNumber];
}
#end
I'm building an app based on a main view controller (CalViewController) to do some math and then, there are two view Controllers (DrinksViewController & FoodViewController).
I build CalViewController and DrinksViewController but when I want to duplicate DrinksViewController to build FoodViewController (same view with different data) a bug appeared : "Property 'bouffon' not found on object of type FoodViewController".
Whereas 'bouffon' defined in my foodViewController.h then imported by CalViewController. It works with 'CaloriesDepensees' (data from DrinksViewController) but not with 'bouffon'. I check every line from DrinksViewController.h and .m and it appears it is exactly the same as FoodViewController.h and .m (with different data obviously).
Custom Class & Storyboard ID is FoodViewController also for this View Controller (in Storyboard, identity inspector).
Big Thanks for your help !
Pierre
Here CalViewController.h :
#import <UIKit/UIKit.h>
#import "CalAppDelegate.h"
#import "DrinksViewController.h"
#import "FoodViewController.h"
#interface CalViewController : UIViewController
{
double temps;
double distance;
double vitesse;
double poids;
NSString *caloriesText;
}
#property (weak, nonatomic) IBOutlet UITextField *saisieTemps;
#property (weak, nonatomic) IBOutlet UITextField *saisieDistance;
#property (weak, nonatomic) IBOutlet UITextField *saisiePoids;
#property (weak, nonatomic) IBOutlet UITextView *resultat;
#property (weak, nonatomic) IBOutlet UISwitch *saisieDefaut;
#property (weak, nonatomic) IBOutlet UILabel *poidsDefaut;
- (IBAction)tempsAction:(id)sender;
- (IBAction)distanceAction:(id)sender;
- (IBAction)defautAction:(id)sender;
- (IBAction)saisieReturn :(id)sender;
- (IBAction)calcul:(id)sender;
- (IBAction)paramSwitch:(id)sender;
- (IBAction)poidsAction:(id)sender;
#end
Here CalViewController.m :
#interface CalViewController ()
#end
#implementation CalViewController
#synthesize resultat;
#synthesize saisieDistance;
#synthesize saisieTemps;
#synthesize saisiePoids;
#synthesize saisieDefaut;
#synthesize poidsDefaut;
double calories;
double factCal;
double vitesse;
double poidsDonne=75;
- (void)viewDidLoad
{
[super viewDidLoad];
calories=1.0;
if (saisieDefaut.on) {
saisiePoids.hidden=YES;
poidsDefaut.hidden=NO;
}
else {
saisiePoids.hidden=NO;
poidsDefaut.hidden=YES;
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)tempsAction:(id)sender {
temps = [[saisieTemps text] doubleValue];
}
- (IBAction)distanceAction:(id)sender {
distance = [[saisieDistance text] doubleValue];
}
- (IBAction)poidsAction:(id)sender {
poids = [[saisiePoids text] doubleValue];
}
- (IBAction)defautAction:(id)sender {
}
- (IBAction)saisieReturn :(id)sender{
[sender resignFirstResponder];
}
- (IBAction)calcul:(id)sender {
temps = [[saisieTemps text] doubleValue];
distance = [[saisieDistance text] doubleValue];
vitesse = distance*1000/temps;
if (vitesse <110)
factCal=1.06;
else if (vitesse < 120)
factCal=1.052;
else if (vitesse < 130)
factCal=1.046;
else if (vitesse < 140)
factCal=1.041;
else if (vitesse < 150)
factCal=1.037;
else if (vitesse < 160)
factCal=1.034;
else if (vitesse < 280)
factCal=1.030;
else if (vitesse < 300)
factCal=1.035;
else if (vitesse < 320)
factCal=1.039;
else if (vitesse < 330)
factCal=1.043;
else if (vitesse < 340)
factCal=1.046;
else if (vitesse < 350)
factCal=1.049;
else if (vitesse < 360)
factCal=1.052;
else if (vitesse < 370)
factCal=1.055;
else if (vitesse < 380)
factCal=1.058;
else if (vitesse < 390)
factCal=1.049;
else if (vitesse>=390)
factCal=1.065;
if (saisieDefaut.on)
calories= factCal*distance*poidsDonne;
else
calories= factCal*distance*poids;
resultat.text = [NSString stringWithFormat:#"%0.f", calories];
}
- (IBAction)paramSwitch:(id)sender {
if (saisieDefaut.on) {
saisiePoids.hidden=YES;
poidsDefaut.hidden=NO;
}
else {
saisiePoids.hidden=NO;
poidsDefaut.hidden=YES;
}
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showFoodSegue"]){
FoodViewController *controller = (FoodViewController *)segue.destinationViewController;
controller.bouffon = calories;
The error appears here, 'bouffon' is not known as an object of FoodViewController.
}
if([segue.identifier isEqualToString:#"showDrinksSegue"]){
DrinksViewController *controllera = (DrinksViewController *)segue.destinationViewController;
controllera.caloriesDepensees = calories;
}
}
#end
Here FoodViewController.h :
#import <UIKit/UIKit.h>
#import "CalAppDelegate.h"
#interface FoodViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate>
#property (weak, nonatomic) IBOutlet UICollectionView *foodCollectionView;
#property(nonatomic) double bouffon;
#end
Here FoodViewController.m :
#import "FoodViewController.h"
#import "FoodCell.h"
#interface FoodViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
NSArray *arrayOfCalories;
double caloriesFoodDouble;
NSString *caloriesFoodObject;
double receptionDouble;
}
#end
#implementation FoodViewController
#synthesize bouffon;
#synthesize foodCollectionView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self foodCollectionView]setDataSource:self];
[[self foodCollectionView]setDelegate:self];
arrayOfImages = [[NSArray alloc]initWithObjects:#"beer2.jpg",#"biere1.png",nil];
arrayOfDescriptions =[[NSArray alloc]initWithObjects:#"bouffe",#"binouffe",nil];
arrayOfCalories =[[NSArray alloc]initWithObjects:#"1000",#"10000",nil];
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{ return [arrayOfDescriptions count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier1=#"Cell1";
FoodCell *cell1 = ([collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier1 forIndexPath:indexPath]);
[[cell1 foodImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
caloriesFoodObject=[arrayOfCalories objectAtIndex:indexPath.item];
caloriesFoodDouble = [caloriesFoodObject doubleValue];
caloriesFoodDouble=caloriesFoodDouble/bouffon;
[[cell1 foodLabel]setText:[NSString stringWithFormat:#"%.1f%#", caloriesFoodDouble,[arrayOfDescriptions objectAtIndex:indexPath.item]]];
return cell1;
}
- (NSInteger)numberOfSections:(UICollectionView *) collectionView
{return 1;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
After some intense hours of research, I figure out that another FoodViewController.m was created and present in my Finder.
So, just have to check my Finder and delete the wrong file.
I have tab bar based app which works great. But I am wanting to use the UIPagecontrol to allow the user to swipe between views.
I have been using the tutorial http://www.samsurge.com/p/blog-page.html to achieve this for my app. But the difference between the tutorial and my app is that my app is based in tab bar system.
The UIscrollview is based on the first tab option with the class pageViewController and the connecting sub views are in IntroViewController.
The storyboard layout looks like this.
http://threepointdesign.co.uk/img2.png
The error generating is
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
Headers for both classes
#import <UIKit/UIKit.h>
#interface simpleMain : UIViewController <UIScrollViewDelegate>
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage:(id)sender;
#end
#import "simpleMain.h"
#interface simpleMain ()
#property (assign) BOOL pageControlUsed;
#property (assign) NSUInteger page;
#property (assign) BOOL rotating;
- (void)loadScrollViewWithPage:(int)page;
#end
#implementation simpleMain
#synthesize scrollView;
#synthesize pageControl;
#synthesize pageControlUsed = _pageControlUsed;
#synthesize page = _page;
#synthesize rotating = _rotating;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollView setPagingEnabled:YES];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
[self.scrollView setShowsVerticalScrollIndicator:NO];
[self.scrollView setDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
[self loadScrollViewWithPage:i];
}
self.pageControl.currentPage = 0;
_page = 0;
[self.pageControl setNumberOfPages:[self.childViewControllers count]];
UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
if (viewController.view.superview != nil) {
[viewController viewWillAppear:animated];
}
self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height);
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= [self.childViewControllers count])
return;
// replace the placeholder if necessary
UIViewController *controller = [self.childViewControllers objectAtIndex:page];
if (controller == nil) {
return;
}
// add the controller's view to the scroll view
if (controller.view.superview == nil) {
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[self.scrollView addSubview:controller.view];
}
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
#end
#import "simpleMain.h"
#interface miniViewController : simpleMain {
}
#property (strong, nonatomic) IBOutlet UIView *View1;
#property (strong, nonatomic) IBOutlet UIView *View2;
#property (strong, nonatomic) IBOutlet UIView *View3;
#end
#import "MiniViewController.h"
#interface miniViewController ()
#end
#implementation miniViewController
#synthesize View1;
#synthesize View2;
#synthesize View3;
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View1"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View2"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View3"]];
}
#end
(I know the error reads, I am trying to find an element inside and empty array. I just don't know where the array is coming from and what connection it has to this setup).
Any help would be great.
From what I see in your storyboard and in your code, the pageViewController doesn't have any childViewControllers when the viewWillAppear is called. So that's causing the crash.
Ditched this tutorial as I found it had to many flaws. Problem solved.
I am a new coder in iOS. When I was trying to code ScrollView with paging, there is an issue to be fixed. I really don't know what is the problem in my code. I already set pageEnable, and set the page number with 4 current 0. In xib.file, I created 4 views, make scrollview connect "delegate" to "file's owner". My reference link http://www.iosdevnotes.com/2011/03/uiscrollview-paging/
When I run the app, the view cannot be paged.
Thank you for your help.
Here is my code:
////////// I think there is a problem in the for loop in viewDidLoad.
import
#interface ThirdViewController : UIViewController {
IBOutlet UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
IBOutlet UIView *contentOne;
IBOutlet UIView *contentTwo;
IBOutlet UIView *contentThree;
IBOutlet UIView *contentFour;
}
- (IBAction)pageValueChange:(id)sender;
#property(nonatomic,strong) IBOutlet UIScrollView *scrollView;
#property(nonatomic,strong) IBOutlet UIView *contentOne;
#property(nonatomic,strong) IBOutlet UIView *contentTwo;
#property(nonatomic,strong) IBOutlet UIView *contentThree;
#property(nonatomic,strong) IBOutlet UIView *contentFour;
#property(nonatomic,strong) IBOutlet UIPageControl *pageControl;
#property(nonatomic,strong) NSArray *viewArray;
#end
#import "ThirdViewController.h"
#interface ThirdViewController ()<UIScrollViewDelegate>
#end
#implementation ThirdViewController
#synthesize scrollView,contentOne, contentTwo,contentThree,contentFour,pageControl,viewArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Profile", #"Third");// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
viewArray = [[NSArray alloc]initWithObjects:#"contentOne",#"contentTwo",#"contentThree", #"contentFour",nil];
for (int i =0; i < [viewArray count]; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i ;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview =[[UIView alloc]initWithFrame:frame];
[self.scrollView addSubview: subview]; //////how to pass the array object to subview
[subview removeFromSuperview];
}
// Do any additional setup after loading the view from its nib.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *[viewArray count], scrollView.frame.size.height);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)dismissKeyboard :(id) sender{
[sender resignFirstResponder];
}
- (IBAction)pageValueChange:(id)sender{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
}
#pragma mark -UIScrollView Delegate
-(void)scrollViewDidScroll:(UIScrollView *)sender{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
self.pageControl.currentPage = page;
}
#end
Did you put a breakpoint next to the line it crashed on?
I have a a class I created to generate UIButton's I add to my UIView. This worked great until my conversion to ARC yesterday, not I get the following error:
-[OrderTypeButton performSelector:withObject:withObject:]: message sent to deallocated instance 0x12449f70
Here is the code to add the button to my UIView (actually a subview in my main UIView):
OrderTypeButton *btn = [[OrderTypeButton alloc]initWithOrderType:#"All Orders" withOrderCount:[NSString stringWithFormat:#"%i",[self.ordersPlacedList count]] hasOpenOrder:NO];
btn.view.tag = 6969;
btn.delegate = self;
[btn.view setFrame:CGRectMake((col * width)+ colspacer, rowHeight + (row * height), frameWidth, frameHeight)];
[self.statsView addSubview:btn.view];
And here is my class header:
#import <UIKit/UIKit.h>
#protocol OrderTypeButtonDelegate
-(void) tapped:(id)sender withOrderType:(NSString*) orderType;
#end
#interface OrderTypeButton : UIViewController {
id<OrderTypeButtonDelegate> __unsafe_unretained delegate;
IBOutlet UILabel *lblOrderType;
IBOutlet UILabel *lblOrderCount;
NSString *orderType;
NSString *orderCount;
BOOL hasOpenOrder;
}
#property (nonatomic, strong) IBOutlet UIButton *orderButton;
#property (nonatomic, strong) IBOutlet UILabel *lblOrderType;
#property (nonatomic, strong) IBOutlet UILabel *lblOrderCount;
#property (nonatomic, strong) NSString *orderType;
#property (nonatomic, strong) NSString *orderCount;
#property (nonatomic, assign) BOOL hasOpenOrder;
#property (nonatomic, unsafe_unretained) id<OrderTypeButtonDelegate> delegate;
-(id) initWithOrderType: (NSString *) anOrderType withOrderCount: (NSString *) anOrderCount hasOpenOrder: (BOOL) openOrder;
-(IBAction)btnTapped:(id)sender;
#end
Implementation:
#import "OrderTypeButton.h"
#implementation OrderTypeButton
#synthesize orderButton;
#synthesize lblOrderType, lblOrderCount, orderType, orderCount, hasOpenOrder, delegate;
-(id) initWithOrderType: (NSString *) anOrderType withOrderCount: (NSString *) anOrderCount hasOpenOrder: (BOOL) openOrder {
if ((self = [super init])) {
self.orderType = anOrderType;
self.orderCount = anOrderCount;
self.hasOpenOrder = openOrder;
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.lblOrderType.text =[NSString stringWithFormat:#"%#", self.orderType];
self.lblOrderCount.text = [NSString stringWithFormat:#"%#", self.orderCount];
if (self.hasOpenOrder) {
[self.orderButton setBackgroundImage:[UIImage imageNamed:#"background-order-btn-red.png"] forState:UIControlStateNormal];
self.lblOrderType.textColor = [UIColor whiteColor];
self.lblOrderCount.textColor = [UIColor whiteColor];
}
}
-(IBAction)btnTapped:(id)sender {
NSLog(#"TAPPED");
if ([self delegate] ) {
[delegate tapped:sender withOrderType:self.orderType];
}
}
- (void)viewDidUnload
{
[self setOrderButton:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#end
This seems fairly simple what I am doing here, not sure what changed with ARC that is causing me problems.
Maybe ARC autorelease created button, try to store created buttons in Array
//.h file
#property (nonatomic, strong) NSArray *buttonsArray
//.m file
#synthesize buttonsArray
...
- (void)viewDidLoad {
buttonsArray = [NSArray array];
...
OrderTypeButton *btn = [[OrderTypeButton alloc]initWithOrderType:#"All Orders"
withOrderCount:[NSString stringWithFormat:#"%i",[self.ordersPlacedList count]]
hasOpenOrder:NO];
btn.view.tag = 6969;
btn.delegate = self;
[btn.view setFrame:CGRectMake((col * width)+ colspacer, rowHeight + (row * height), frameWidth, frameHeight)];
[self.statsView addSubview:btn.view];
//Add button to array
[buttonsArray addObject:btn];
Also this approach will help if you want to change buttons, or remove some specific button from view