How to remove subclass of UILabel in itself - objective-c

A subclass of UILabel called NewLabel, in NewLabel.m
+ (NewLabel*)addLabelIntoView:(UIView*)view
{
NewLabel *label = [[NewLabel alloc] init];
CGSize size = CGSizeMake(120.0f, 40.0f);
CGPoint point = CGPointMake(view.bounds.size.width / 2, view.bounds.size.height / 2);
label.frame = CGRectMake(0, 0, size.width, size.height);
label.center = point;
[view addSubview:label];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(removeLabelFromView) userInfo:nil repeats:NO];
return label;
}
- (void)removeLabelFromView {
[self removeFromSuperview];
}
and in a UIViewController.m, I add this for showing the NewLabel, and close itself
[NewLabel addLabelIntoView:self.view]
but it always crash, here is crash info
+[NewLabel removeLabelFromView]: unrecognized selector sent to class 0x103d40738
Any ideas?

In a class method is self the class. Instead of +[NewLabel removeLabelFromView] you probably want to do -[NewLabel removeLabelFromView]. Change
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:#selector(removeLabelFromView) userInfo:nil repeats:NO];
to
[NSTimer scheduledTimerWithTimeInterval:1.0f target:label selector:#selector(removeLabelFromView) userInfo:nil repeats:NO];

Related

Is it possible to have a UITextView scroll through text automatically, and repeatedly in Xcode?

Is it possible to have a UITextView scroll through text automatically, and repeatedly? Like, I have a UITextView, and the text just scrolls, and scrolls, and when it has reached the end, repeats itself, over and over, without the user having to scroll?
- (void)viewDidLoad
{
h=0; // add the variable int h to your interface
myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(scrollPosition)
userInfo:nil
repeats:YES];
}
- (void) scrollPosition {
h += 50; // add the variable int h to your interface
//you must add the textview delegate to file's owner
[self.textview setContentOffset:CGPointMake(0, h) animated:YES];
}
-(void)scrollViewDidScroll: (UIScrollView*)scrollView
{
float scrollViewHeight = scrollView.frame.size.height;
float scrollContentSizeHeight = scrollView.contentSize.height;
float scrollOffset = scrollView.contentOffset.y;
// at the end of scrollview
if (scrollOffset + scrollViewHeight >= scrollContentSizeHeight)
{
//you must add the textview delegate to file's owner
[self.textview setContentOffset:CGPointMake(0, 0) animated:YES];
[myTimer invalidate];
myTimer = nil;
h=0;
myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:self
selector:#selector(scrollPosition)
userInfo:nil
repeats:YES];
}
}

Display an image for 1 sec on the view

What I want to realize is:
When I touch a button, the image display on the view for 1 sec, then the image disappear.
I know that a NSTimer will help, but i dont know how to write the right code...need your help, thanks.
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(showPictures:) userInfo:nil repeats:NO];
}
- (void)showPictures:(NSTimer *)timer {
bodytouchedimage.hidden = YES;
}
What you should to is call the showPictures function when you touch the button and then within the showPictures method you add a NSTimer that will call a hidePictures method 1 second later
- (void)showPictures
{
bodytouchedimage.hidden = NO;
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(hidePictures) userInfo:nil repeats:NO];
}
- (void)hidePictures
{
bodytouchedimage.hidden = YES;
}
Rather than using NSTimer, it would be easier to simply call your method to hide the image like this:
- (IBAction)bodytouched:(id)sender {
bodytouchedimage.hidden = NO;
bodytouchedimage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"beated.jpg"]];
bodytouchedimage.userInteractionEnabled = YES;
[self performSelector:#selector(hidePicture) withObject:nil afterDelay:1];
}
- (void)hidePicture {
bodytouchedimage.hidden = YES;
}
performSelector:withObject:afterDelay: is a method of the NSObject class.

NSTimer and UIView Memory Mistake

i create views and release all objects. i used NSTimer and
//at viewdidload
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 15.0
target: self
selector:#selector(selectorSwitcher:)
userInfo: nil repeats:YES];
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:timer forMode: NSDefaultRunLoopMode];
-(void)selectorSwitcher:(NSTimer *)timer {
[self viewDidLoad];
}
but myviews can not delete from scrollview. Creating a more views up to others. How can i resolve this problem?
Thanks
if (tempView.frame.origin.x > createview.frame.size.width) {
tempLabel = [[UILabel alloc] initWithFrame:CGRectMake((lblname_marginleft + (i * widthOfView))-((i/sutun)*mview_width), lblname_margintop + ((i/sutun) * heightOfView), labelwidth, labelheight)];
tempNumber = [[UILabel alloc] initWithFrame:CGRectMake((lblnumber_marginleft + (i* widthOfView))-((i/sutun)*mview_width), lblnumber_margintop + ((i/sutun) * heightOfView), labelwidth, labelheight)];
tempyuzde= [[UILabel alloc] initWithFrame:CGRectMake((15 + (i*widthOfView))-((i/sutun)*mview_width), 10 + ((i/sutun) * heightOfView), labelwidth, labelheight)];
tempView = [[UIView alloc] initWithFrame:CGRectMake((10 + (i* widthOfView)) - ((i/sutun)*mview_width), ((i/sutun) * heightOfView), widthOfView, heightOfView)];
You should not be calling viewDidLoad - the system calls it once the view has loaded. To load the view, simply do:
UIView *view = self.view;
Calling self.view implicitly loads the XIB file. However the system also loads the view automatically when it is time to do so - what functionality are you trying to achieve?
Also, you should not be calling
NSRunLoop *runner = [NSRunLoop currentRunLoop];
[runner addTimer:timer forMode: NSDefaultRunLoopMode];
Since by calling scheduledTimerWithTimeInterval... you've already scheduled the timer - you don't need to schedule it again.

Pausing between image views

I want to run a series of pictures with dialog bubble (like a strip cartoon). I've tried to use the action:
-(IBAction) runDialog:(id)sender {
imageView.image = [UIImage imageNamed:#"a1.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"b1.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"b2.png"];
[NSThread sleepForTimeInterval:5.0];
imageView.image = [UIImage imageNamed:#"a2.png"];
[NSThread sleepForTimeInterval:5.0];
}
This doesn't work. All it does is show the last image (a2.png) After About 20 seconds
Any ideas how I should go about this:showing a series of pictures with a pause in between?
Use NSTimer instead.
- (IBAction)runDialog:(id)sender {
yourInstanceVariableOfNSTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self selector:#selector(showNextPage) userInfo:nil repeats:NO];
}
- (void)showNextPage {
imageView.image = [yourInstanceVariableOfNSArray
objectAtIndex:++yourInstanceVariableOfNSInteger];
if (!transitionsFinished) {
yourInstanceVariableOfNSTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f
target:self selector:#selector(showNextPage) userInfo:nil repeats:NO];
}
}
A simpler way to handle this would be to use a UIImageView in the dialog and set its animationImages, animationDuration, etc... properties.

Objective C: Passing variables between classes

I want to pass an NSMutableDictionary from one class through a timer to another class that is called from the timer. I get an error at #selector([generateStickFig:enemies]) I've tried taking [] out but it still errs.
-(void)awakeFromNib {
//start timer that fires every second
NSMutableDictionary *enemies = [[NSMutableDictionary alloc] init];
[NSTimer scheduledTimerWithTimeInterval:(0.001) target:self selector:#selector([generateStickFig:enemies]) userInfo:nil repeats:YES];
}
-(void) generateStickFig:(NSMutableDictionary *)enemies {
int x = random() % 1000;
if (x == 1) {
stickFig = [[UIButton alloc] initWithFrame:CGRectMake(0, 650, 50, 50)];
[stickFig setBackgroundColor:[UIColor blackColor]];
[stickFig addTarget:self action:#selector(tapFig:) forControlEvents:UIControlEventTouchUpInside];
[enemies setObject:object forKey:[NSString stringWithFormat:#"object%i",i]];
[self.view addSubview:stickFig];
}
}
Any suggestions?
Try
[NSTimer scheduledTimerWithTimeInterval:(0.001) target:self selector:#selector(generateStickFig:) userInfo:enemies repeats:YES];
to create the timer. Then change the callback method to
-(void) generateStickFig:(NSTimer *)timer {
NSDictionary *enemies = (NSDictionary *)timer.userInfo;
...
}