SimpleView Based app with id initWithFrame Not working - iphone

I have app to create a graph i have created a view based app and then add the code for creating graph in it but it does not disply the graph. If use same code to create a separate UIView then it works other wise not
#import <UIKit/UIKit.h>
#import "ECGraph.h"
#import "ECGraphItem.h"
#class GraphsViewController;
#interface Display : UIView {
NSArray *percentages;
int myY;
ECGraph *graph;
ECGraphItem *item1;
ECGraphItem *item2;
}
#property(nonatomic,retain)NSArray*percentages;
-(void) setPercentageArray:(NSArray*) array;
#end
#import "Display.h"
#import "ECGraph.h"
#implementation Display
#synthesize percentages;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef _context = UIGraphicsGetCurrentContext();
graph = [[ECGraph alloc] initWithFrame:CGRectMake(500,-320,320, 200) withContext:_context isPortrait:NO];
item1 = [[ECGraphItem alloc] init];
item2 = [[ECGraphItem alloc] init];
/*
ECGraphItem *item1 = [[ECGraphItem alloc] init];
ECGraphItem *item2 = [[ECGraphItem alloc] init];*/
item1.isPercentage = YES;
item1.yValue=myY;
item1.width = 35;
item1.name = #"item1";
item2.isPercentage = YES;
item2.yValue =17;
item2.width = 35;
item2.name = #"item2";
[graph setXaxisTitle:#"name"];
[graph setYaxisTitle:#"Percentage"];
[graph setGraphicTitle:#"Histogram"];
[graph setDelegate:self];
[graph setBackgroundColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1]];
NSArray *items = [[NSArray alloc] initWithObjects:item1,item2,nil];
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}
I am adding this view in GraphsViewController but its not showing anything
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self createGraph];
percentages = [NSArray arrayWithObjects:#"80",#"17", nil];
display = [[Display alloc] init];
[self.view addSubview:display];
[display setPercentageArray:percentages];
}

You should do the drawing in a separate UIView object and add it as a subview to your view controller's view. That's the way it is supposed to work.
The UIView class uses an on-demand drawing model for presenting
content.
Source: View Programming Guide for iOS
as opposed to
The UIViewController class provides the fundamental view-management
model for all iOS apps. ... A view controller manages a set of views
that make up a portion of your app’s user interface.
Source: UIViewController Class Reference
Edit:
// ...
display = [[Display alloc] init];
CGRect dFrame = CGRectMake(50, 50, 320, 200); // change these to whatever values you need
[display setFrame:dFrame];
[self.view addSubview:display];

Related

how to load a UIView on button click

I have Seprate UIView which i am using in UIViewController it initially loads but i want that when that is loaded and I again click on the button then it should reload because it has graph creation method which draws graph when view loaded
#import <UIKit/UIKit.h>
#import "ECGraph.h"
#import "ECGraphItem.h"
#import "CereniaAppDelegate.h"
#class GraphsViewController;
#interface Display : UIView {
NSArray *percentages;
CereniaAppDelegate*appDelegate;
}
#property(nonatomic,retain)NSArray*percentages;
-(void) setPercentageArray:(NSArray*) array;
#end
Implementation files
#import "Display.h"
#import "ECGraph.h"
#import "CereniaAppDelegate.h"
#implementation Display
#synthesize percentages;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef _context = UIGraphicsGetCurrentContext();
ECGraph *graph = [[ECGraph alloc] initWithFrame:CGRectMake(70,-70,800,200) withContext:
_context isPortrait:NO];
appDelegate=[[UIApplication sharedApplication]delegate];
ECGraphItem *item1 = [[ECGraphItem alloc] init];
ECGraphItem *item2 = [[ECGraphItem alloc] init];
ECGraphItem *item3 = [[ECGraphItem alloc] init];
item1.isPercentage = YES;
int value=(int)roundf(appDelegate.graphValueOne);
item1.yValue=value;
item1.width = 70;
item1.name = #"Unvaccinated horse";
int value1=(int)roundf(appDelegate.graphValueTwo);
item2.isPercentage = YES;
item2.yValue=value1;
item2.width = 70;
item2.name = #"Annually Vaccinated horse";
int value3=(int)roundf(appDelegate.graphValueThree);
item3.isPercentage = YES;
item3.yValue=value3;
item3.width = 70;
item3.name = #"Semi-Annually vaccinated horse";
NSArray *items = [[NSArray alloc] initWithObjects:item1,item2,item3,nil];
[graph setXaxisTitle:#""];
[graph setYaxisTitle:#"Risk"];
[graph setDelegate:self];
[graph setBackgroundColor:[UIColor lightGrayColor]];
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}
-(void) setPercentageArray:(NSArray*) array
{
percentages = array;
NSString*test=[percentages objectAtIndex:0];
NSLog(test);
}
- (void)dealloc {
[super dealloc];
}
#end
You can get any view to redraw with
[view setNeedsDisplay];
So on your refresh button you would update the data used by the Display class and then setNeedsDisplay.

Tapping on image i m getting EXC_BAD_ACCESS

Guys, i thought i found the solution but no.... i making app cont. from past 16 hrs, may be that's y my empty mind is not making any good sound... plz help me..
I am setting Tag to my dynamically generated imageviews and setting tapgestureReconizers on that,tags are given to UITapGestureRecognizer, i want to perform different actions on click on different images, but when ever click on any image it says very silently... :| EXC_BAD_ACCESS.
Code: -
#import <UIKit/UIKit.h>
#class AppDelegate_iPhone,Litofinter,ParsingViewController;
#interface FirstViewController : UIViewController<UIGestureRecognizerDelegate>{
NSMutableArray *array;
NSString *logoString;
AppDelegate_iPhone *appDelegate;
ParsingViewController *obj;
UIScrollView *scrollView;
NSMutableArray *idArray;
}
#property (nonatomic,retain)UIScrollView *scrollView;
-(void)onTapImage:(UITapGestureRecognizer *)recognizer;
#end
#import "FirstViewController.h"
#import "AppDelegate_iPhone.h"
#import "Litofinter.h"
#import "ParsingViewController.h"
#implementation FirstViewController
#synthesize scrollView;
-(id)init{
if(self == [super init]){
obj = [[ParsingViewController alloc] init];
array = [[NSArray alloc] initWithArray: obj.LogoMutableArray];
}
return self;
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
int x=20,y=10;
int a=50,b=105;
appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,500, 460)];
scrollView.contentSize = CGSizeMake(320,800);
scrollView.showsVerticalScrollIndicator = YES;
for (Litofinter *lito in appDelegate.logoArray) {
NSString * urlString = [lito.cLogo stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL * imageURL = [NSURL URLWithString:urlString];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView *imgView = [[UIImageView alloc] initWithImage:image];
[imgView setFrame:CGRectMake(x, y, 140, 90)];
imgView.userInteractionEnabled = YES;
imgView.multipleTouchEnabled = YES;
imgView.backgroundColor = [UIColor blueColor];
// imgView.tag = lito.cId;
// NSLog(#"Tag Id = %#",imgView.tag);
NSLog(#"Tag Id = %#",lito.cId);
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onTapImage:)];
tgr.delegate = self;
[imgView addGestureRecognizer:tgr];
[scrollView addSubview:imgView];
tgr.view.tag =(int)[NSString stringWithFormat:#"%#",lito.cId];
NSLog(#"Tag Id = %#",tgr.view.tag);
// NSLog(#"Tag Id = %#",lito.cId);
UILabel *cName = [[UILabel alloc]initWithFrame:CGRectMake(a, b, 130, 20)];
cName.text = lito.cName;
[scrollView addSubview:cName];
//Do the rest of your operations here, don't forget to release the UIImageView
x = x + 150;
a = a + 140;
if(x >300)
{
y = y +140;
x = 20;
b = b +150;
a = 50;
}
//[tgr release];
[imgView release];
}
[self.view addSubview:scrollView];
}
-(void)onTapImage:(UITapGestureRecognizer *)recognizer;
{
NSLog(#"Tapped Image tag: %#", recognizer.view.tag);
//NSLog(#"Tapped Image Id ======================== %#",scrollView.gestureRecognizers);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Message from mAc" message:[NSString stringWithFormat:#"Tag Id : %#",recognizer.view.tag] delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Ok",nil];
[alert show];
}
- (void)dealloc {
[super dealloc];
[scrollView release];
}
#end
This could be due to you trying to log an int as a string in your -(void)onTapImage:(UITapGestureRecognizer *)recognizer selector:
NSLog(#"Tapped Image tag: %#", recognizer.view.tag);
instead, replace %# with %i:
NSLog(#"Tapped Image tag: %i", recognizer.view.tag);

UITabBarController's TabBar is slightly hidden

My UITabBarController's tabBar is slightly off the view, please can you tell me what is wrong with my code:
LoggedInViewController *lvc = [[[LoggedInViewController alloc]
initWithAccount:account] autorelease];
[self presentModalViewController:lvc animated:YES];
- (void)viewDidLoad
{
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController;
navController = [[LoggedInFeedNavigationController alloc]
initWithAccount:self.account];
[self.tabController setViewControllers:
[NSArray arrayWithObject:navController]];
[self.view addSubview:self.tabController.view];
[super viewDidLoad];
}
You are adding the tabController view as a subview, but you have not specified where it should be located within its parent view, or how it should be resized when the parent view changes size. Try the following:
- (void)viewDidLoad
{
[super viewDidLoad]; // see note
self.tabController = [[UITabBarController alloc] init];
LoggedInFeedNavigationController *navController;
navController = [[LoggedInFeedNavigationController alloc]
initWithAccount:self.account];
[self.tabController setViewControllers:
[NSArray arrayWithObject:navController]];
UIView *tabView = self.tabController.view;
[self.view addSubview:tabView];
tabView.frame = self.view.bounds;
tabView.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
}
Note: you are not required to call [super viewDidLoad], but if you do decide to call it, you should call it at the beginning of your viewDidLoad method, and not at the end.

UITabBarContrtoller achieve from a different class in iPhone

I have the following problem:
There is a class that includes five tabs in the following way:
mainMenuClient.h
#import <UIKit/UIKit.h>
#interface MainMenuClient : UIViewController {
UITabBarController *tabBarController;
}
#property (nonatomic, retain) UITabBarController *tabBarController;
#end
mainMenuClient.m
-(void)viewDidLoad {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor blackColor];
self.view = contentView;
[contentView release];
ContactListTab *contactTab = [[ContactListTab alloc] init];
ChatTab *chat = [[ChatTab alloc]init];
DialerTab *dialer = [[DialerTab alloc]init];
MenuTab *menu = [[MenuTab alloc]init];
TesztingFile *teszting = [[TesztingFile alloc]init];
contactTab.title = #"Contact List";
chat.title = #"Chat";
dialer.title = #"Dialer";
menu.title = #"Menu";
teszting.title = #"TesztTab";
contactTab.tabBarItem.image = [UIImage imageNamed:#"Contacts_icon.png"];
chat.tabBarItem.image = [UIImage imageNamed:#"Chat_icon.png"];
dialer.tabBarItem.image = [UIImage imageNamed:#"Dialer_icon.png"];
menu.tabBarItem.image = [UIImage imageNamed:#"Menu_icon.png"];
teszting.tabBarItem.image = [UIImage imageNamed:#"Contacts_icon.png"];
chat.tabBarItem.badgeValue = #"99";
tabBarController = [[UITabBarController alloc]init];
tabBarController.view.frame = CGRectMake(0, 0, 320, 460);
[tabBarController setViewControllers:[NSArray arrayWithObjects:contactTab, chat, dialer, menu, teszting, nil]];
[contactTab release];
[chat release];
[dialer release];
[menu release];
[teszting release];
[self.view addSubview:tabBarController.view];
[super viewDidLoad];
}
In the contactTab class there are a UITableViewController.
contactTab.h
- (void)updateCellData;
- (void)configureCell:(UITableViewCell *)cell forIndexPath:(NSIndexPath *)indexPath;
There is a third class, which I would like to achieve is a method of UITableViewController's (from ContactTab).
So far I tried this:
When I tried to achieve the UItabbarController:
MainMenuClient *menu;
UITabBarController *tabBarControllerchange = [[UITabBarController alloc] init];
tabBarControllerchange = menu.tabBarController;
[tabBarControllerchange setSelectedIndex:0];
When I tried to achieve the UITableViewController:
ContactListTab *contactListTab;
[contactListTab updateCellData];
Does anybody have an idea for this problem? Thanks. Balazs.
You need to get the instance of your MainMenuClient:
define method in your MainMenuClient.h as:
+(MainMenuClient*)getMainMenuInstance;
Implement the following method in MainMenuClient.m as :
+(MainMenuClient*)getMainMenuInstance
{
return self;
}
Now you can get same instance of UITabBarContrtoller in any class as :
MainMenuClient *menuClient = [MainMenuClient getMainMenuInstance];
UITabBarContrtoller *newTabBarController = menuClient.tabBarController;
You can do what ever you want to do with this new UITabBarController object.
Hope this helps.
Jim.

Why does my TTTableViewController not appear

I have an application which contains a scrollview with two subviews (to scroll left and right between them)
Both views appear correctly and scrolling between the views worked fine. Now I want to change the first view to be a TTTableView (courtesy of Three20) but when I use the class 'TableControlsTestController' from the TTCatalog application all I see is an empty tableView
Note that this class contains all the data to display 6 cells
To add the new TTTableView I use the following
[scrollView addSubview:detailView.view];
where detailView is the instance of TableControlsTestController
To try and narrow down where the problem is I also tried calling
[self presentModalViewController:detailView animated:YES];
This correctly displays the tableview with the 6 cells.
Why when I try to add the view to the scrollView does this not work as I expect?
For reference if you do not have access to the TTCatalog
#import "TableControlsTestController.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
#implementation TableControlsTestController
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)init {
if (self = [super init]) {
self.tableViewStyle = UITableViewStyleGrouped;
self.autoresizesForKeyboard = YES;
self.variableHeightRows = YES;
UITextField* textField = [[[UITextField alloc] init] autorelease];
textField.placeholder = #"UITextField";
textField.font = TTSTYLEVAR(font);
UITextField* textField2 = [[[UITextField alloc] init] autorelease];
textField2.font = TTSTYLEVAR(font);
textField2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
TTTableControlItem* textFieldItem = [TTTableControlItem itemWithCaption:#"TTTableControlItem"
control:textField2];
UITextView* textView = [[[UITextView alloc] init] autorelease];
textView.text = #"UITextView";
textView.font = TTSTYLEVAR(font);
TTTextEditor* editor = [[[TTTextEditor alloc] init] autorelease];
editor.font = TTSTYLEVAR(font);
editor.backgroundColor = TTSTYLEVAR(backgroundColor);
editor.autoresizesToText = NO;
editor.minNumberOfLines = 3;
editor.placeholder = #"TTTextEditor";
UISwitch* switchy = [[[UISwitch alloc] init] autorelease];
TTTableControlItem* switchItem = [TTTableControlItem itemWithCaption:#"UISwitch" control:switchy];
UISlider* slider = [[[UISlider alloc] init] autorelease];
TTTableControlItem* sliderItem = [TTTableControlItem itemWithCaption:#"UISlider" control:slider];
self.dataSource = [TTListDataSource dataSourceWithObjects:
textField,
editor,
textView,
textFieldItem,
switchItem,
sliderItem,
nil];
}
return self;
}
#end
It turns out that some of the events were not being propagated through the scrollView and therefore not handled in the TTTableViewController. To fix this I had to do the following in my scrollView
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[myNewTTViewController viewWillAppear:NO];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[myNewTTViewController viewDidAppear:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[myNewTTViewController viewWillDisappear:NO];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[myNewTTViewController viewDidDisappear:NO];
}
As soon as I did this the table popped into life