I have two UITextViews, self.instructions and self.textView, that are supposed to alternate depending on what the user selects.
I create a self.textView like so:
-(void)createSpaceToWrite
{
[self.instructions removeFromSuperview];
[self.bar removeFromSuperview];
[self createNavigationBar:#"Compose" selector:#"displayScreen" withDone:NO]; //This adds a UINavigationBar to the view.
if (!self.textView)
{
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 60, 280, 150)];
}
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.font = [UIFont fontWithName:#"Helvetica Neue" size:14];
self.textView.text = #"";
[self.view addSubview:self.textView];
self.textView.delegate = self;
}
Then I create self.instructions like so:
-(void)haikuInstructions
{
[self.textView removeFromSuperview];
[self.bar removeFromSuperview];
[self createNavigationBar:#"Compose" selector:#"displayScreen" withDone:NO];
if (!self.instructions)
{
self.instructions = [[UITextView alloc] initWithFrame:CGRectMake(10, 125, [[UIScreen mainScreen] bounds].size.width - 10, [[UIScreen mainScreen] bounds].size.height)];
}
self.instructions.backgroundColor=[UIColor clearColor];
self.instructions.text = #"Text of instructions";
self.instructions.editable=NO;
[self.view addSubview:self.instructions];
[self resignFirstResponder];
}
The user starts with self.instructions displayed against the background image. Fine.
The user switches. The instruction text disappears, to be replaced by the editable self.textView, a white box. Fine.
The user switches back. The instruction text appears--but the white box is still there, even thought I've removed it from the superview. And not only that, it's still editable and still brings up the keyboard when the user goes to edit it!
What am I doing wrong?
EDIT: Well, I basically scrapped all the code and started the class over from scratch, trying to be cleaner about everything, and I'm no longer having this problem, so it must have been something in some other method that was affecting it. Lesson: haphazard coding is bad!
Why do you need to remove your text views interchangeably? Wouldn't it be better to just "hide" them interchangeably by setting the setHidden property like for example:
[self.textView setHidden: YES];
and additionally try the following also:
[self.textView resignFirstResponder];
[self.textView setEditable: NO];
[self.textView setBackgroundColor: [UIColor clearColor]];
[self.textView setAlpha: 0.0];
Related
This is my code. I can see the small dialog box appeared then the dialog take over the whole screen. What is wrong?
ChangeProfileImage *changeProfileImage =[[ChangeProfileImage alloc] init];
changeProfileImage.modalPresentationStyle = UIModalPresentationFormSheet;
changeProfileImage.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:changeProfileImage animated:YES];
changeProfileImage.view.superview.frame = CGRectMake(0, 0, 200, 200);//it's important to do this after
changeProfileImage.view.superview.center = self.view.center
I was all ready to tell you everything that's wrong with this code, but then I set up a simple iPad single view application and tried the following code:
- (IBAction)FlipPressed:(id)sender {
UIViewController *vc = [[UIViewController alloc] init];
UIView *view = [vc view];
[view setBackgroundColor:[UIColor redColor]];
[vc setModalPresentationStyle:UIModalPresentationFormSheet];
[vc setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentModalViewController:vc animated:YES];
[[[vc view] superview] setFrame:CGRectMake(0, 0, 200, 200)];
[[[vc view] superview] setCenter:[[self view] center]];
}
When I try this, I get a red box, 200x200, with rounded corners, that flips into view centered on screen.
As far as I can tell, I've replicated your code pretty much exactly, and I think I got the result you want, right?
EDIT/UPDATE
Further testing appears to indicate that this code will only work well when the iPad is in the portrait orientation. Otherwise, my view is appear to flip "vertically" (with respect to how the iPad is oriented) and the view isn't centered.
-(void)onTimer{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(400, 100, 200, 200)];
[myLabel setBackgroundColor:[UIColor clearColor]];
myLabel.text = #"Button1 is in range";
[myLabel setTextAlignment:UITextAlignmentCenter];
[myLabel setTextColor:[UIColor whiteColor]];
NSLog(#"x=%f",Object1.center.x);
//position = CGPointMake(0,0);
//Object1.center = CGPointMake(Object1.center.x,Object1.center.y);
if((Object1.center.x >341) && (Object1.center.x < 597)){
[myLabel setHidden:NO];
}
else {
[myLabel setHidden:YES];
}
[self.view addSubview:myLabel];
}
This is the code I am using now. The x coordinates I get don't change when the object moves. Does anyone know how can I do that? Thanks.
I'd bet that you're calling the onTimer method before the object is moved. Can't tell from the code you've written above, but it'd be worth a shot to check for that.
Maybe you should call this method when the object is done dragging, instead of just when it starts dragging?
I'm trying to display editable NSTextField on Mac in Cocos2d project. I run all my test code directly from AppDelegate to make test simple and eliminate all side effects. The code bellow shows red window with nice edit field. The trouble is that the field looks like without focus (text selection is gray) and is not editable (no cursor and no reaction to key strokes). I can change the text selection with mouse but that is all.
Any hint to what's wrong?
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
CCDirectorMac *director = (CCDirectorMac*) [CCDirector sharedDirector];
[glView_ setFrameSize:NSMakeSize(1024,768)];
[director setOpenGLView:glView_];
[director setResizeMode:kCCDirectorResize_AutoScale];
[glView_ setFrameSize:NSMakeSize(window_.frame.size.width,window_.frame.size.height-20)];
[window_ setContentAspectRatio:NSMakeSize(1024,768)];
[window_ setAcceptsMouseMovedEvents:NO];
[self addOverlayWindow];
//[director runWithScene:[MCGameScene scene]/*[HelloWorldLayer scene]*/];
}
- (void) addOverlayWindow;
{
NSRect windowRect = [[window_ contentView] frame] ;
NSWindow* uiWindow = [[NSWindow alloc] initWithContentRect:windowRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered defer:YES];
[uiWindow setBackgroundColor: [NSColor redColor/*clearColor*/]];
[uiWindow setOpaque:NO];
NSView* uiView = [[[NSView alloc] initWithFrame:NSMakeRect(0, 0, windowRect.size.width, windowRect.size.height)] autorelease];
[uiView translateOriginToPoint:NSMakePoint(100, uiView.bounds.size.height/2)];
uiView.wantsLayer = YES;
[uiWindow setContentView:uiView];
NSTextField *textField;
textField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 800, 80)];
[textField setFont:[NSFont fontWithName:#"Helvetica Bold" size:60]];
[textField setStringValue:#"My XXXXXXXXX Label"];
[textField setBezeled:YES];
[textField setDrawsBackground:YES];
[textField setEditable:YES];
[textField setSelectable:YES];
[textField setEnabled:YES];
[uiView addSubview:textField];
// None of these combinations works...
// [textField becomeFirstResponder];
// [uiWindow setInitialFirstResponder:textField];
// [uiWindow makeFirstResponder:textField];
//[window_ makeFirstResponder:textField];
[window_ setInitialFirstResponder:textField];
// [uiView setInitialFirstResponder:textField];
// [uiView makeFirstResponder:textField];
[window_ addChildWindow:uiWindow ordered:NSWindowAbove];
}
Thanks in advance for your help,
--Josef
I have a pretty funny problem which involves loadView, viewDidLoad, viewWillAppearand the state of an ivar. I have a navigation based app. And from my first level view I have a table list and then I click on one of the cells it will take me to second level view (detail view). When I click on a cell I also add an "Office" object (which contains strings like streetAddressand boxAddress) to the view controller that gets pushed. I then populate the detailed view with the contents from the Office object like [box setText:[self.office boxAddress]]; (box is a UILabel). Now what I want to achieve here is that sometimes the stringValue of boxAddress is empty and in those cases I don't want to add an empty string to the UILabel, instead I want to move the next UILabel up (and take the place of the boxAddress). So therefore I've made a conditional check to see if boxAddress is empty if it is it should set up UILabels with specific coordinates and if it's not empty it should set up UILabels with other specific coordinates.
I understand that you should use viewWillAppear if you want the code to be run everytime the view is loaded. But for some reason it seems that viewWillAppear is only ran when the boxAddress string is noy empty. And if I click on a cell that has an empty boxAddress it will use the value from boxAddressfrom the last cell that I clicked that had a non-empty boxAddress.
I'll paste my code here to see if you can give me a pointer on what I'm doing wrong here.
// Implement loadView to create a view hierarchy programmatically,
// without using a nib.
- (void)loadView {
//allocate the view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//set the view's background color
self.view.backgroundColor = [UIColor whiteColor];
}
- (void)viewDidLoad
{
//add the labels
name = [[UILabel alloc] initWithFrame:CGRectMake(10.0,10.0,320.0,20.0)];
[name setBackgroundColor:[UIColor clearColor]];
[name setTextColor:[UIColor blackColor]];
street = [[UILabel alloc] initWithFrame:CGRectMake(10.0,30.0,320.0,20.0)];
[street setBackgroundColor:[UIColor clearColor]];
[street setTextColor:[UIColor blackColor]];
//if no box address, move up the rest of the addresses
if ([[self.office boxAddress] length] == 0) {
zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,50.0,320.0,20.0)];
[zip setBackgroundColor:[UIColor clearColor]];
[zip setTextColor:[UIColor blackColor]];
phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,320.0,20.0)];
[phone setBackgroundColor:[UIColor clearColor]];
[phone setTextColor:[UIColor blackColor]];
fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,320.0,20.0)];
[fax setBackgroundColor:[UIColor clearColor]];
[fax setTextColor:[UIColor blackColor]];
[self.view addSubview:name];
[self.view addSubview:street];
[self.view addSubview:zip];
[self.view addSubview:phone];
[self.view addSubview:fax];
} else {
box = [[UILabel alloc] initWithFrame:CGRectMake(10.0,50.0,320.0,20.0)];
[box setBackgroundColor:[UIColor clearColor]];
[box setTextColor:[UIColor blackColor]];
zip = [[UILabel alloc] initWithFrame:CGRectMake(10.0,70.0,320.0,20.0)];
[zip setBackgroundColor:[UIColor clearColor]];
[zip setTextColor:[UIColor blackColor]];
phone = [[UILabel alloc] initWithFrame:CGRectMake(10.0,90.0,320.0,20.0)];
[phone setBackgroundColor:[UIColor clearColor]];
[phone setTextColor:[UIColor blackColor]];
fax = [[UILabel alloc] initWithFrame:CGRectMake(10.0,110.0,320.0,20.0)];
[fax setBackgroundColor:[UIColor clearColor]];
[fax setTextColor:[UIColor blackColor]];
[self.view addSubview:name];
[self.view addSubview:street];
[self.view addSubview:box];
[self.view addSubview:zip];
[self.view addSubview:phone];
[self.view addSubview:fax];
}
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
// viewWillAppear method is run every time the view is loaded as opposite to the viewDidLoad method which only is run once
// in this program DisclosureDetail view needs to be loaded for each detail view with different content each time
- (void)viewWillAppear:(BOOL)animated {
NSLog(#"%#", [self.office boxAddress]);
[name setText:[self.office name]];
[street setText:[self.office streetAddress]];
if ([[self.office boxAddress] length] > 0) {
[box setText:[self.office boxAddress]];
}
[zip setText:[NSString stringWithFormat:#"%# %#", [self.office zipCode], [self.office city]]];
[phone setText:[self.office phoneNo]];
[fax setText:[self.office faxNo]];
[super viewWillAppear:animated];
}
if ([[self.office boxAddress] length] > 0) {
[box setText:[self.office boxAddress]];
}
If boxAddress's length is 0 then box will continue to contain whatever text you set in it the last time the view appeared.
You're going to need to hide and show box every time the view appears not just when the view is loaded because the view might be loaded and then used to display multiple different offices.
I am dynamically adding a NSTextField to a window and I am having issues with rendering. I am setting the background color to be black and the text color to be white. These both work but their is what appears to be a rectangle that is part of the text that is always white. Does anyone know what I might be doing wrong? How can I get rid of the white background that is just around the text? Code is as follows:
//Create rectangle to size text field
NSRect textFieldRect = NSMakeRect(300, 300, 300, 54);
//Instantiate text field and set defaults
NSTextField* textField = [[NSTextField alloc] initWithFrame:textFieldRect];
[textField setFont:[NSFont fontWithName:#"Arial" size:48]];
[textField setTextColor:[NSColor whiteColor]];
[textField setStringValue:#"Some Text"];
[textField setBackgroundColor:[NSColor blackColor]];
[textField setDrawsBackground:YES];
[textField setBordered:NO];
[[window contentView] addSubview:textField];
I tried your code on Mac OS X 10.6.4.
Inside the application delegate:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSRect textFieldRect = NSMakeRect(300, 300, 300, 54);
NSTextField* textField = [[NSTextField alloc] initWithFrame:textFieldRect];
[textField setFont:[NSFont fontWithName:#"Arial" size:48]];
[textField setTextColor:[NSColor whiteColor]];
[textField setStringValue:#"Some Text"];
[textField setBackgroundColor:[NSColor blackColor]];
[textField setDrawsBackground:YES];
[textField setBordered:NO];
[[window contentView] addSubview:textField];
}
And this is the result
alt text http://www.freeimagehosting.net/uploads/26c04b6b64.png
I can't see any white box.
Maybe you are using a different OS.
Or maybe you have some other views on top of each other that are causing the weird effect you are talking about.
Try setting refusesFirstResponder = TRUE property of your NSTextField object. I have come across behavior you described in 10.7, in 10.6 everything works as expected.
Ok,
The mystery is partially solved. In conjunction with my NSTextField, I am also setting some NSApplicationPresentationOptions to put the application into Kiosk mode. It appears that something with that is causing the problem I am seeing. If I do not set the PresentationOptions the NSTextField displays exactly the way I want it to. I will track down what specific PresentationOption is to blame and post here.