Help with Xcode drawRect using textfield to input value - xcode

i am currently trying to do a bar graph in XCode. I have tried CPGraph and all the stuff around but they are all out of date and i need help for XCode 4. I am completely newb with this and that's why i need your help.
Here is my code:
-(void)drawRect:(CGRect)rect
{
NSString *s = textField.text;
value = [s intValue];
NSLog(#"%i",value);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 60);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, 400, value);
CGContextAddLineToPoint(context, 400, 100);
CGContextStrokePath(context);
CGColorSpaceRelease(colorspace);
CGColorRelease(color);
}
Notice that i have changed one of the point value for "value".
What i have done is create a UITextField and i want to be able to write like 500 in the UITextField and that the bar adjust itself. Currently the NSLog tell me that value is equal to 0 even if, before building my app i manually enter a number in the TextField.
I have been searching fo 3 days now and everything i found give me errors and are incomplete as i says i know almost nothing about objective-c in xcode. I also noticed during my search that this type of line doesn't refresh real time if you dont tell him. I would like help with that too if that's part of my problem. If you want more information on my code just tell me.
I will be really grateful if somebody can help me.
here is my .h :
#interface draw2D : UIView
{
UITextField *textField;
}
#property (nonatomic, retain) IBOutlet UITextField *textField;
#end

OK, it looks like since you have your text field hooked up in IB to File's Owner, that is typically your app delegate which is not the class you have above as that is a UIView. So, not unless you changed File's Owner to this UIView then your text field is not hooked up. So, typically what I do is to add a UIView to the xib window in IB (if you are not using the one that comes free when you create a XIB). Then that is what I'll attach the text box to, not File's Owner. Kind of begs the question of how anything is attached to Files Owner not unless you have a UITextField declared in there too (again providing you didn't change the class from the app delegate to this view class).
I know that if someone is new to this is is almost like greek. I developed in Java, C/C++, C#, etc for years and this was like learning how to program all over again.
If you are new to this I highly suggest the iPhone dev book by Big Nerd Ranch. I found it very useful.

OK, I found out how to redraw my line with a button refresh.
What I did was create a button and link it with my UIView where my line is being drawn. Inside I wrote [self setNeedsDisplay]; and each time I click the button it refreshes the view and gives me back my NSlog that I set in the same method as my drawRect.
Many thanks again Merky.

Related

xcode change label's text after presentViewController

as always relying on this community wich is always really helpful!
I really don't understand why my code isn't working, i am playing the game in ViewControllerA for example, there is "movements" and more stuff shown,wich are labels with int variables shown, this ones give me no problem because they change correctly,when the game is completed i want to show the score and more...
So i created another ViewController to do it, ViewControllerB for the example, the thing is that A and B Viewcontrollers belong to the SAME class, so i should be able to use the same variables, methods and everything ( actually i do that already and it's working fine)
I created IBOutlet for some labels, this ones belong to B. I am using presentViewcontroller method to show it up,the labels don't get anything changed, here is the code:
NSString *moviments = [NSString stringWithFormat:#"%d",TotalMovements];
NSString *score = [NSString stringWithFormat:#"%d",TotalPoints];
NSString *levelToShow = [NSString stringWithFormat:#"%d",PlayingLevel];
[movementsToShowOnCompletedScreen setText:moviments];
[scoreToShowOnCompletedScreen setText:score];
[levelToShowOnCompletedScreen setText:levelToShow];
UIStoryboard *winOFLevelStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *vc = [winOFLevelStoryboard instantiateViewControllerWithIdentifier:#"mainGame"];
[self presentViewController:vc animated:YES completion:^{
/*I have tried to add the same code from above setting the labels text
labels here, same result...
*/
}];
The labels don't get its text changed.
This is it, i hope with all of this you can find out what's going on here, the IBOutlets are fine, i even changed programmatically the frame of those labels before so that can't be the problem, outlets are fine.
If you need more code or anyhthing just let me know!
Thanks for your time !
Well, i found it...just in case anyone has the same problem. It solves it but not the way i wanted to so if anyone knows the good way please let me know.
Since those belong to the same class i have just copied/pasted this lines shown below to my viewdidload method, now labels are changing when they have to but i really think this is a really poor solution.
Thanks again!

UITextView scroll to bottom without animation

I want the UITextView to show the last text so I did
[self.textBox scrollRangeToVisible:NSMakeRange([self.textBox.text length], 0) ];
However, I don't want the scrolling action since it will start from the very top and scroll down each time I enter in a new line of text.
How do I go about doing this?
UITextView inherits from UIScrollView, so you can call the latter's methods directly on it.
CGPoint offsetPoint = CGPointMake(0.0, textView.contentSize.height - textView.bounds.size.height);
[textView setContentOffset:offsetPoint animated:NO];
P.S. In your question you wrote UITextField, but it was clear from your description and code that you meant UITextView. I edited the question to reflect this.

Can't change TableView size

In my.m file:
#synthesize ListLearnTab;
...........
- (void)viewDidLoad
{
[super viewDidLoad];
ListLearnTab.frame=CGRectMake(0, 0, 320, 416);
}
where ListLearnTab I declared in .h as:
#property (weak, nonatomic) IBOutlet UITableView *ListLearnTab;
I spend many hours and don't know why TableView doesn't want resize. I put this ListLearnTab.frame=CGRectMake(0, 0, 320, 416); in many places, but TableView stay as I declare in Xcode (manualy).
I had the same problem, I think. I set the size of the table in the code, like you. I could see it on the story board. The prototype cell looked correct, but only seemed to go halfway across the table when the app ran. I dragged the entire view controller out bigger on the storyboard, clicked on the view itself and dragged it out, then clicked on the table and did the same. If I recall, I changed the widths of everything in the inspector to make them larger than the frame specification in viewDidLoad.
I got everything working. Sorry about not remembering the exact step that solved the problem.
If that's the same as your trouble, how this helps! -Rob

Can't change Text Label on Sheet

I've created a sheet which I would like to display various messages while the program is doing data crunching. The sheet opens and closes correctly and I have a Text Label on the sheet which is connected to my main controller (the owner of the sheet) with an IB Outlet.
The Nib loads correctly, open and closes correctly, but the static text label is never updated. The connected IBOutlet was defined this way:
IBOutlet id mySheetText;
The call I am using to try and modify the text on the sheet is this:
[mySheetText setStringValue:#"Some text message..."];
This format works fine if the Label is in the main window, but does nothing if the Label is on the sheet.
All the connections in IB appear to be correct. I'm sure that I'm missing something very simple, and i'm guessing that it has something to do with the fact that the sheet is a different "window" than the main window, but I can't seem to find anything in the docs to point me in the right direction.
Incidentally, here is the way I connected the sheet...
NSWindow *mySheet;
#property (assign) IBOutlet NSWindow *mySheet;
#synthesize mySheet;
...and opened it:
if (!serverSyncSheet) {
[NSBundle loadNibNamed:#"mySheetNibFile" owner:self];
}
[NSApp beginSheet: self.mySheet
modalForWindow: [[NSApp delegate] mainWindow]
modalDelegate: self
didEndSelector: NULL
contextInfo: NULL];
Any ideas?
* EDIT *
Ok, so it turns out that I am partially correct. If I attempt to read the text value, it turns out that it IS setting it to the correct text, but the sheet is not being updated to DISPLAY the change. I suspect that I need to tell the window to redraw... never done that before. Back to the docs to see if I can find it. If anyone knows the method to call, let me know. :) Thanks!
Well, my suspicions were correct... it was something simple.
I just had to redraw the window:
[mySheet display];
Doh! :)

How to customize / style a UIPopoverController

I'm working on an iPad application and I'm using UIPopoverControllers. I'm at the part where the app needs to be branded and styled and i'm wondering how to change the color / tint of the UIPopoverController? Standard is dark blue but it needs to be another color..
is this possible?
Greets, Thomas
This is possible starting in iOS 5.0 by subclassing the abstract class UIPopoverBackgroundView and assigning your subclass to the popoverBackgroundViewClass property on your UIPopoverController instance. Unfortunately there is no tintColor property as the popover needs to use images for it's arrow and border in order to achieve smooth animations during dynamic resizing. You can learn more about how to customize the appearance of a UIPopoverController in the UIPopoverBackgroundView Class Reference
It's impossible for now.
It's what I call the "Box in a Box" model. You get control of the box inside of the box (the UIViewController inside of the UIPopoverController), but you have very limited control over the actual popover itself. Outside of the arrow direction and the size, you can't change much else. There are also options for a modal effect popover, which dims everything else when it shows up, but I haven't tried to get it working.
I'm sure you've noticed there is no UIPopover class by now.
The answer you want to hear:
If you really want to style one that bad, just write your own. It's really not that hard.
The link you want to click:
Cocoacontrols is an index of iOS and OSX components available on GitHub, they have some popover stuff.
iOS 7 introduces backgroundColor property of UIPopoverController which affects/includes the navigation background color as well as arrows of popover.
#property (nonatomic, copy) UIColor *backgroundColor NS_AVAILABLE_IOS(7_0);
Usage example:
if ([self.popoverVC respondsToSelector:#selector(setBackgroundColor:)]) { // Check to avoid app crash prior to iOS 7
self.popoverVC.backgroundColor = [UIColor greenColor]; // [UIColor colorWithPatternImage:#"..."] doesn't reflect the color on simulator but on device it works!
}
Note - As of now (iOS 7.0.3), in some cases (like set color using colorWithPatternImage:), the simulator (and even some devices) doesn't honor the color.
Throwing my hat in here;
I've leveraged UIPopoverBackgroundViews in iOS 5+ to add a simple tintColor property onto UIPopoverControllers.
PCPopoverController: https://github.com/pcperini/PCPopoverController
I try to trick it by customizing the view controller inside the popover and then hiding the popover border using this code:
UIView * border = [[insideViewController.view.superview.superview.superview subviews] objectAtIndex:0];
border.hidden = YES;
The app is actually still in development so I'm hoping other people will comment on this solution.
check out these latest projects leveraging UIPopoverBackgroundView
https://github.com/CRedit360/C360PopoverBackgroundView
https://github.com/GiK/GIKPopoverBackgroundView
from ios 5 onward it is can be done, here is a library
https://github.com/ddebin/DDPopoverBackgroundView
just look at the documentation , and it is quite easy
good luck
You can use Elegant Popover cocoapod for just that. You can customise shape and colour of the arrow and the popover itself. Also, you can add colourful borders to the popover.
I know this is a lousy constructed answer, but I've just been playing with the UIPopoverController's views. They do exist.
The only way to access them is from your view that is sitting in the UIPopovercontroller.
I have a navigation controller so I follow this hierarchy
UIView *test = ((UIView *)[[[self.navigationController.view.superview.superview.subviews objectAtIndex:0] subviews] objectAtIndex:1]);
UIView *test2 = ((UIView *)[[[self.navigationController.view.superview.superview.subviews objectAtIndex:0] subviews] objectAtIndex:1]);
test.backgroundColor = [UIColor greenColor];
test2.backgroundColor = [UIColor greenColor];
This isn't exactly the end goal, but it is really close.
you'll find that the_view_in_the_popover.superview.superview (maybe just one superview if you are not reaching out from a navigation controller view) is a UIPopoverView. If you cast it as a UIView and treat it as a UIView you're not really breaking any rules. I guess that is really up to apple though.
Remove UIPopoverController border:
NSArray* subviews = ((UIView*)[popupController.contentViewController.view.superview.superview.superview.subviews objectAtIndex:0]).subviews;
for(UIView *subview in subviews){
[subview removeFromSuperview];
}

Resources