how to make it so when a view loads, it does something? - xcode

I am making a quiz app, and I wanted it to have a saying at the end of the quiz. This saying is supposed to change based on an NSInteger (myScore). How would I code this? Any help would be helpful to me. Thanks!

You are going to create a UILabel based on an NSInteger:
NSInteger yourInteger;
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100);
[aLabel setText:#"Score %d", yourInteger];
[aLabel setBackgroundcolor:[UIColor clearColor]];
[aLabel setOpaque:NO];
[[self view] addSubview:aLabel];
actually %d is replaced with your integer

Related

last row of uicollectionview is not visible while scrolling

I created UiCollectionView programmatically. This is my code snippet...
UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc] init];
collectionV=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 500) collectionViewLayout:layout];
[collectionV setDataSource:self];
[collectionV setDelegate:self];
[collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:#"cellIdentifier"];
[collectionV setBackgroundColor:[UIColor greenColor]];
collectionV.scrollEnabled = YES;
[self.view addSubView:CollectionV];
My problem is I could not see the last row while scrolling. Anybody help me to fix it.
Thanks in advance.

Expanding and Shrinking UIView in iPhone

I am working on an iPhone games like application, where I have to ask one question and corresponding answers on UIButoons. When user presses any button I show right and wrong image based on chosen answer. I create answer view by following code and attach it to main view:
-(void)ShowOutputImageAsAnswerView:(BOOL)correct
{
viewTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:#selector(RemoveSubViewFromMainView) userInfo:nil repeats:YES];
UIView *viewOutput = [[UIView alloc]initWithFrame:CGRectMake(200, 100, 80, 80)];
viewOutput.tag = 400;
viewOutput.backgroundColor = [UIColor clearColor];
UIImageView *imgAns = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
if (correct)
{
imgAns.image = [UIImage imageNamed:#"correct_icon.png"];
}
else
{
imgAns.image = [UIImage imageNamed:#"wrong_icon.png"];
}
[viewOutput addSubview:imgAns];
[self.view addSubview:viewOutput];
[self.view bringSubviewToFront:viewOutput];
[imgAns release];
[viewOutput release];
}
I have to show this answer view in the animated form. And this animation will start from its 0 pixel to 80 pixel. Means expanding and shrinking view.
How can it be implemented?
Can it directly be used with imgAns?
You create an animation by doing something like this:
myView.frame=CGRectMake(myView.frame.origin.x,myView.frame.origin.y,0.,0.);
[UIView animateWithDuration:1
animations:^{
//put your animation here
myView.frame=CGRectMake(myView.frame.origin.x,myView.frame.origin.y,80.,80-);
}];
I haven't tried this code, but it should give you a good perspective of how to do it.

UITapGestureReconizer error

I am getting errors with my UITapGestureReconizer like "UITapGestureReconizer undeclared". Because UITapGestureReconizer is inside a concrete class should it not be declared? and how do I fix it... below is where it first shows up in the code.
- (void)loadView {
[super loadView];
// set the tag for the image view
[imageView setTag:ZOOM_VIEW_TAG];
// add gesture recognizers to the image view
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[imageView addGestureRecognizer:doubleTap];
[doubleTap release];
UITapGestureRecognizer *twoFingerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTwoFingerTap:)];
[imageView addGestureRecognizer:twoFingerTap];
[twoFingerTap release];
[doubleTap setNumberOfTapsRequired:2];
[twoFingerTap setNumberOfTouchesRequired:2];
// calculate minimum scale to perfectly fit image width, and begin at that scale
float minimumScale = [imageScrollView frame].size.width / [imageView frame].size.width;
[imageScrollView setMinimumZoomScale:minimumScale];
[imageScrollView setZoomScale:minimumScale];
}
UITapGestureRecognizer is a part of UIKit, so it should be declared as long as you’re importing <UIKit/UIKit.h> somewhere. One problem I see in your code, though, is this:
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[imageView addGestureRecognizer:doubleTap];
[doubleTap release];
…
[doubleTap setNumberOfTapsRequired:2];
At the point where you call [doubleTap setNumberOfTapsRequired:2], you don’t have a valid reference to doubleTap. It’s most likely retained by imageView, but that’s an implementation detail that you can’t rely on. Move the call to -setNumberOfTapsRequired: before the call to -release to ensure that this doesn’t break in a future version of iOS.

Xcode - UILabel is not changing value on viewDidAppear

Hey everyone, I have a iPhone App I am creating. It uses a uitableview and that goes to a detail view.
Now on the detail view I have a scroll view, and all the data (XML) comes into it fine, each time, no issues.
My UILabels in my scrollview do update with the correct data, but, they keep adding subviews on top of each other to keep adding label after label and it all looks mumbo jumbo.
I have tried everything to try and remove the subviews such as:
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
AND
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
So I am stumped, I got no idea what is happening. Any help? I have been searching Google for ages and agesm, but keep finding the same answers that don't work :(
Any direction to sites that might help that you know of will be greatly appreciated.
Thanks!
Here is what I have in my controller that loads all the scrollview and labels:
- (void)viewDidAppear:(BOOL)animated {
// Create the scroll view for this view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 270);
[scrollView setFrame:CGRectMake(0, 198, 320, 170)];
// Do the labels for the text
UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];
dates.textColor = [UIColor whiteColor];
dates.font = [UIFont boldSystemFontOfSize:12];
dates.text = round_date;
dates.tag = 1;
[dates setBackgroundColor:[UIColor clearColor]];
[dates setFrame:CGRectMake(10, 10, 280, 22)];
[scrollView addSubview:dates];
[dates release];
// Add the content to the main scrollview
[self.view addSubview:scrollView];
[scrollView release];
}
Ok, the reason for this is that viewDidAppear will get called every time you present the view.
If you do this is
-(void) viewDidLoad;
instead.
To make sure you can access them later you might want to keep a reference to them in your UIViewController
-(void) viewDidLoad {
scrollView = [[UIScrollView alloc] initWithFrame];
[self.view addSubview:scrollView];
}
and define it in your header file with:
UIScrollView *scrollView;

Program doesn't jump in Delegate-Method

Good Morning Community,
I have a problem, because I have implemented an CoverFlowViewController.m with an Delegate method, but the Debugger NEVER jump in this method, does anyone know why?
Here is the code:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *weiter = [UIButton buttonWithType:UIButtonTypeRoundedRect];
weiter.frame = CGRectMake(100, 400, 120, 40);
[weiter addTarget:self action:#selector(goToChart) forControlEvents:UIControlEventTouchUpInside];
NSString *ansicht = #"Weiter";
[weiter setTitle:ansicht forState:UIControlStateNormal];
[self.view addSubview:weiter];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
NSString *imageName;
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
}
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
- (void)openFlowView:(AFOpenFlowView *)openFlowView selectionDidChange:(int)index{
NSLog(#"%d is selected",index);
}
Does anyone know why the Debugger (= the Programm) never jump into the selectionDidChange() method?
I have no solution since three days, and hope somebody could help me?
Greetings and Thank you beforehand
Marco
are you sure you connected the viewDelegate to your viewController?
Please add NSLog(#"Delegate: %#", ((AFOpenFlowView*)self.view).viewDelegate); at the end of your viewDidLoad method to see if it is connected.
Add
((AFOpenFlowView *)self.view).viewDelegate = self; on the init or viewDidLoad method

Resources