How to read set coordinates for swiping - objective-c

-(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?

Related

Can't see the button

- (void)viewDidLoad{
int leftBorder = 80;
int topBorder = 160;
int width = 150;
int height = 50;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(leftBorder, topBorder, width, height)];
myView.layer.cornerRadius = 5;
myView.backgroundColor = [UIColor redColor];
[self.view addSubview:myView];
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testButton.frame = CGRectMake(0, 0, 50, 50);
[testButton setTitle:#"testButton" forState:UIControlStateNormal];
[testButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myView addSubview:self.testButton];
self.myView.hidden = YES;
[super viewDidLoad];
}
Hi, sorry for stupid question! I'm newby in xcode. Why I didn't see this button? And How can I hide button after click? I need button inside the frame.
Simple remove self.myView.hidden = YES;
To add you click listener, two solution:
By code in your viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[mybutton addTarget:self action:#selector(myButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
}
- (void)myButtonClick:(id)sender {
myButton.hidden = YES;
}
Or via interface Builder (preferred), The easiest way is to actually define the handler/action in Xcode using the IBAction declaration in the interface file (add the declaration just before the #end statement). Then attach the action to the button
you are adding self.testButton instead of the created testButton.
[self.myView addSubview:testButton];
you did not assign myView to your property.
[self.view addSubview:myView]; self.myView = myView;
remove self.myView.hidden = YES;
Another remark:
You should usually call super as early as possible. Otherwise, the superclass may interfere with your own implementation.
There are several issues in your code.
You add testButton as subview to self.myView. Then you hide self.myView. As a result neither self.myview nor its subviews will be visible.
You get confused with your local varialbles and instance varialbes. Apparently there is an instance variable and property myView. Otherwise you coudn't use self.myView. And you declare a local variable myView, which is not the same as the instance variable. This may well be perfectly ok. But I got a guts feeling that you did this not intentionally. Your self.myView may well be nil at the point in time where you add the subview. And even the subview self.testButton may be nil. That will compile file and execute fine but nothing does happen actually.
The same for testButton.
I suggest to change the code a bit, assuming there are properties for myView and testButton of the appropriate types:
self.myView = [[UIView alloc] initWithFrame:CGRectMake(leftBorder, topBorder, width, height)];
self.myView.layer.cornerRadius = 5;
self.myView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.myView];
self.testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.testButton.frame = CGRectMake(0, 0, 50, 50);
[self.testButton setTitle:#"testButton" forState:UIControlStateNormal];
[self.testButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.myView addSubview:self.testButton];
self.myView.hidden = NO;

UITextView doesn't disappear when removedFromSuperview

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];

iOS edit UIButton properties

I am working on the basics of iOS development.
So far I have a button that on press, will show some text. But what I want to do is after it is pressed, I want it to then change the text of the button, so far this is what I have:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.backgroundColor = [UIColor redColor];
button.titleLabel.textColor=[UIColor blackColor];
button.frame = CGRectMake(25, 100, 275, 60);
[button setTitle:#"Press this button to reveal the text!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(button_method:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)button_method:(UIButton *)sender {
NSString *test = #"I am learning Objective-C for the very first time! Also, this is my first ever variable!";
// handle button press
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(25, 25, 275, 60)];
label.text = test;
label.numberOfLines = 0;
label.lineBreakMode = UILineBreakModeWordWrap;
//label.lineBreakMode = NSLineBreakByWordWrapping; //iOS 6 only
[self.view addSubview:label];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
When I try adding [button setTitle:#"You pressed the button"]; to button_method
This doesn't work... why? And how would I make it work?
When I try adding [button setTitle:#"You pressed the button"]; to button_method, it doesn't work... why?
Because that method is nonexistent on UIButton.
And how would I make it work?
By using a method that UIButton actually responds to. For example:
[sender setTitle:#"You pressed the button" forState:UIControlStateNormal];
And read the relevant documentation, please.
Your code will not change the title of the button. But simply add a label as a subview for your view. Is this what you want?
If you want to change the text on the button you will want to do sender.title.text = test; in your button_method
I also recommend adding a NSLog(#"Button pressed"); to your button press method to double check that it is being called.

UILabel text not changing?

Unable to change text in lMenu_time (this is a UILabel) after it was initially set.
The call back is executed, I tested this, but the text won't change.
?? I am passing around the pointer and making adjustments to the UILabel. ??
lMenu_time and numerous others are defined in the header file. (not seen here)
UILabel *lMenu_time;
...
-(void) NewNumber: (UIButton*) btn {
if (btn.tag == 102){
iTime++;
[lbl setText:#"time"];
if(iTime > 20){iTime=1;}
[lMenu_time setText:[NSString stringWithFormat: #"Hold: %d", iTime]];
}
....
}
- (void) menuItem: (UIView*)vMenu menuButton:(UIButton*)bMenu menuLabel: (UILabel*)lMenu menuPosX: (double)posX menuLenX: (double)lenX menuTagNum: (int)tagNum menuText: (NSString*)txtMenu{
bMenu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[bMenu setFrame:CGRectMake(posX,0,lenX,25)];
[bMenu setTag: tagNum];
[bMenu addTarget:self action:#selector(NewNumber:) forControlEvents:UIControlEventTouchUpInside];
[vMenu addSubview:bMenu];
lMenu = [[[UILabel alloc] initWithFrame:CGRectMake(posX,0,lenX,25)] retain];
[lMenu setBackgroundColor:[UIColor lightGrayColor]];
[lMenu setText:[NSString stringWithFormat: txtMenu]];
[lMenu setFont:[UIFont systemFontOfSize:14 ]];
[lMenu setTextAlignment:UITextAlignmentCenter];
[vMenu addSubview: lMenu];
}
- (void) menuBuild{
pSelf = self;
theString = #"";
UIView *vMenu = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,25)];
[pSelf.view addSubview:vMenu];
[vMenu setBackgroundColor:[UIColor grayColor]];
iTime = 2;
[self menuItem:vMenu menuButton:bMenu_time menuLabel:lMenu_time menuPosX:240+20 menuLenX:60 menuTagNum:102 menuText:[NSString stringWithFormat: #"Hold: %d", iTime]];
...
}
Just before you try to set the next, try adding
NSLog(#"My label is %#",lMenu_time);
Then, if your console outputs "My label is (nil)" you'll know that the problem is that the pointer to lMenu_time isn't being passed around properly.
Did you bind the label object to the controller in Interface builder? If not I would bet this is an retention issue. You do not post the code that builds the UILabel object, so if that is not done in IB, ensure you call retain or it will certainly be out of scope when you try and modify it.

Controls created on runtime has no window. Popovers cannot be presented from a view which does not have a window

i have button on the view and call following void to open popover controller with image picker, no problem here popover come up with image picker etc.
when i create UIButton programmatically on viewDidLoad the buttons calls same void below however this time that throws an exception "Popovers cannot be presented from a view which does not have a window."
-(IBAction)pickupPhoto:(id)sender
{
CGRect rect = CGRectMake(100, 100, 1, 1); // [(UIScrollView *)sender frame];
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.popOver = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
[popOver setDelegate:self];
[popOver presentPopoverFromRect:rect inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionUp animated:NO];
[imgPicker release];
}
here i am creating buttons on same view buttons call the void.
-(void)addButtons
{
for (int i = 0; i < 10; i++) {
self.btn = [[UIButton alloc] initWithFrame:CGRectMake(i * 100, 0, 100, 80)];
[[self.btn layer] setBorderColor:RGB(245, 245, 245).CGColor];
[[self.btn layer] setBorderWidth:1.0f];
[[self.btn titleLabel] setFont:[UIFont fontWithName:#"Thonburi" size:70]];
[self.btn setTitle:doubleToString(i+1) forState:UIControlStateNormal];
[self.btn setTitleColor:RGB(245, 245, 245) forState:UIControlStateNormal];
[self.btn setTitle:doubleToString(i+1) forState:UIControlStateSelected];
[self.btn setTitleColor:RGB(125, 225, 225) forState:UIControlStateHighlighted];
[self.btn addTarget:[NMBPostUI alloc] action:#selector(pickupPhoto:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
}
}
i can see when i call -(void)addButtons, self.view.window returns nil for the buttons created at runtime that causes the problem. yes i should create buttons during init but there is no init call when you call view controller as -storyBoard- Page Sheet. how can i walk around this?
any ideas are welcome.
many thanks.
Try creating buttons in:
initWithNibName:bundle:
or
awakeFromNib
methods. Maybe this will work.