PageControl page handling - iphone

I am trying to use a UIPageControl with my UIScrollView. It seems that i'm doing something improperly cause the page is always set to one.
This is my ViewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView.delegate = self;
UIImageView *imgView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tutorialScreen1.png"]];
imgView1.frame = CGRectMake(0, 0, 320, 436);
[scrollView addSubview:imgView1];
UIImageView *imgView2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tutorialScreen2.png"]];
imgView2.frame = CGRectMake(320, 0, 320, 436);
[scrollView addSubview:imgView2];
UIImageView *imgView3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"tutorialScreen3.png"]];
imgView3.frame = CGRectMake(640, 0, 320, 436);
[scrollView addSubview:imgView3];
scrollView.contentSize = CGSizeMake(960, 436);
pageControl = [[UIPageControl alloc] init];
[pageControl setNumberOfPages:3];
[pageControl setCurrentPage:0];
}
And here is my UIScrollView Delegate Method:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollViewx{
NSLog(#"%f",scrollView.contentOffset.x);
NSLog(#"%f",(scrollViewx.contentOffset.x / 320));
pageControl.currentPage = ((scrollViewx.contentOffset.x / 320));
}
And here is y .h file:
#interface TutorialViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic,strong) IBOutlet UIScrollView *scrollView;
#property (nonatomic,strong) IBOutlet UIPageControl *pageControl;
#end
Does anyone have an idea why it doesn't change? Of course i made sure my UIPageControl is connected to its instance.
Appreciate your help...

OK. the dot isn't changing because you've created a new UIPageControl instead of using the one you set up in IB. Take out: pageControl = [[UIPageControl alloc] init];

Related

Create new UI elements programmatically

I'd like to be able to create elements (like UIViews) when for example the user touches a button
NSMutableString *myVar = [NSMutableString stringWithFormat:#"_view%i", num];
UIView * newView = [self valueForKey:myVar];
but without adding all the
UIView * _view1;
UIView * _view2;
...
in the .h file (if only this is possible..)
You can use an NSMutableArray to hold them. Each time you create a new view just add it to the array.
Here's sample code that should do what you want.
#interface MyViewController ()
#property (strong, nonatomic) NSMutableArray *listChildViews;
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.listChildViews = [[NSMutableArray alloc] init];
}
- (IBAction)addChildViewTapped:(id)sender
{
int numChildViews = [self.listChildViews count];
++numChildViews;
// add new child view
NSString *labelForNewView = [NSString stringWithFormat:#"view %d", numChildViews];
CGFloat labelHeight = 28.0;
UILabel *childView = [[UILabel alloc] initWithFrame:CGRectMake(10, numChildViews*labelHeight, 120.0, labelHeight)];
childView.text = labelForNewView;
[self.listChildViews addObject:childView];
[self.view addSubview:childView];
}
#end
Here is code implementation of pauls answer:
- (IBAction)userTouchedButton:(id)sender{
for (int i = 0; i < 100; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor redColor]];//to distinguish theseViews from other views
[view setTag:i];//to identified it later
[_array insertObject:view atIndex:i];// globleMutble array
[self.view addSubview:view];
}
}
You need not add your views in the .h file. Just instantiate before and where you add them
-(void) addButton
{
UIView *view = [self view];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setTitle:#"My Button" forState:UIControlStateNormal];
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, 0, 0)];
[myLabel setText:#"My Label"];
[button1 sizeToFit];
[myLabel sizeToFit];
[view addSubview:button1];
[view addSubview:myLabel];
}

UIView in NSCache being overriden

I have a UIView that contains drop shadows and corners which I am loading four of in my UIViewController and I am seeing a performance hit when the screen loads. Since I am using the same white background with shadows and corner radii I figured I would store the UIView in NSCache.
When I run the app there is a large gap where the first UIView should be, however, it is not showing up. What does show up is the last view in my list of views. If I comment out the last one and run it again, the third one shows up. It seems like I am having an issue with the pointer in memory but not sure. Perhaps I am using NSCache incorrectly?
(Note: The first view shown is not using the NSCache)
Here is how I am using the NSCache:
.h file
#interface LunchDetailViewController : UIViewController <UIScrollViewDelegate>
#property (nonatomic) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) NSCache *entreeViewsCache;
#end
.m file
#synthesize scrollView;
#synthesize entreeViewsCache;
- (void)viewDidLoad
{
[super viewDidLoad];
self.entreeViewsCache = [[NSCache alloc] init];
UIView *entreeView = [[UIView alloc] init];
entreeView.backgroundColor = [UIColor whiteColor];
entreeView.layer.masksToBounds = NO;
entreeView.layer.cornerRadius = 3.0;
entreeView.layer.shadowOffset = CGSizeMake(1.1, 2.1);
entreeView.layer.shadowOpacity = 0.2;
[self.entreeViewsCache setObject:entreeView forKey:#"EntreeView"];
}
- (void) configureScrollView
{
// This line of code allows the scroll view to be 'scrollable'.
self.scrollView.contentSize = CGSizeMake(320, 620);
UIView *elementaryRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
elementaryRoundedCornerView.frame = CGRectMake(15,15,290,180);
UIView *middleRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
middleRoundedCornerView.frame = CGRectMake(15,210,290,180);
UIView *highRoundedCornerView = [self.entreeViewsCache objectForKey:#"EntreeView"];
highRoundedCornerView.frame = CGRectMake(15,404,290,180);
NSMutableArray *entreeItems = [[NSMutableArray alloc] initWithObjects:#"Pancakes w/ Sausage Patties", #"Corn Dog", #"Grilled Cheese Sandwhich", #"Chicken Tender Wraps", nil];
UIView *elementaryLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 240, 160)];
[elementaryLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 5, 215, 160) schoolType:#"Elementary"]];
[elementaryRoundedCornerView addSubview:elementaryLunchMenuDetails];
UIView *middleLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 240, 160)];
[middleLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 2, 215, 160) schoolType:#"Middle"]];
[middleRoundedCornerView addSubview:middleLunchMenuDetails];
UIView *highLunchMenuDetails = [[UIView alloc] initWithFrame:CGRectMake(10,10, 240, 160)];
[highLunchMenuDetails addSubview:[self returnNativeCode:entreeItems rectDimensions:CGRectMake(2, 2, 215, 160) schoolType:#"High"]];
[highRoundedCornerView addSubview:highLunchMenuDetails];
[self.scrollView addSubview:elementaryRoundedCornerView];
[self.scrollView addSubview:middleRoundedCornerView];
[self.scrollView addSubview:highRoundedCornerView];
}
Wow. That's clever. But not correct.
Instead of using NSCache to duplicate a view, you probably want to create a UIView subclass that formats the view the way you want. Then just throw a bunch of those views on your scrollview.
ABCView.m
#implementation ABCDayView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
self.layer.masksToBounds = NO;
self.layer.cornerRadius = 3.0;
self.layer.shadowOffset = CGSizeMake(1.1f, 2.1f);
self.layer.shadowOpacity = 0.2f;
}
return self;
}
- (void)setItems:(NSArray *)items
{
if ([_items isEqualToArray:items] == NO) {
_items = items;
[self createItemViews];
[self setNeedsLayout];
}
}
// You'll also need to add -createItemViews and -setNeedsLayout methods.
.m file
- (void)configureScrollView
{
NSMutableArray *entreeItems = #[#"Pancakes w/Sausage Patties",
#"Corn Dog",
#"Grilled Cheese Sandwhich",
#"Chicken Tender Wraps"];
CGRect frame = CGRectMake(15,15,290,180);
ABCDayView *elementaryView = [[ABCDayView alloc] initWithFrame:frame];
elementaryView.items = entreeItems;
CGFloat y = CGRectGetMaxY(elementaryView.frame) + 10.0f;
frame = CGRectMake(15, y, 290, 180);
ABCDayView *middleView = [[ABCDayView alloc] initWithFrame:frame];
middleView.items = entreeItems;
...
CGFloat length = // Use CGRectGetMaxY on the last frame to get the length.
self.scrollView.contentSize = CGSizeMake(320, length);
}
That's by no means perfect code. But hopefully it will give you an idea of a better way to implement this.

Simple Custom UIView Class Broken

I have made a simple subclass of UIView which has a few subviews but its breaking for a reason I can't see.
In my storyboard view controller I have added a UIView and made it the shape I want it, and added my custom class to it. The contents of my subclass is below:
#implementation PersonDetailCell
#synthesize button, requiredMarker, textField;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void)layoutSubviews
{
[self setBackgroundColor:[UIColor colorWithRed:87.0f/255.0f green:153.0f/255.0f blue:191.0f/255.0f alpha:1.0f]];
textField = [[DetailTextField alloc] initWithFrame:CGRectMake(10, 0, self.frame.size.width -20, self.frame.size.height)];
[textField setFont:[UIFont fontWithName:#"Helvetica" size:14.0f]];
[textField setTextColor:[UIColor whiteColor]];
[textField setHidden:YES];
[self addSubview:textField];
button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setHidden:YES];
[self addSubview:button];
requiredMarker = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 4, 22)];
[requiredMarker setBackgroundColor:[UIColor redColor]];
[requiredMarker setHidden:YES];
[self addSubview:requiredMarker];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Then in my view controller code I have imported my subclass and hooked up an IBOutlet of it to my view in my storyboard. In my viewDidLoad of my view controller I also try setting background colour of this view but nothing happens.
All of my code runs without crashing, but my issues:
I can't set the background colour from my view controller, (or any other properties for that matter).
When I run the app, the height is more than that I set in my storyboard (width is fine), but I change the height of the view nowhere.
Any ideas? Am i using the wrong approach somehow?
Thanks.
If it is a table cell, then the class should be extending UITableViewCell. Buttons and textfields can be added on the storyboard - then you can use struts and springs (Size Inspector pane) to ensure the view resizes properly. I don't think layoutSubviews should be used for adding new UI elements (maybe for resizing if you need to do it manually).
Here is some working code:
#interface ChildView()
#property (strong, nonatomic) UITextField *textField;
#property (strong, nonatomic) UIButton *button;
#property (strong, nonatomic) UIImageView *requiredMarker;
#end
#implementation ChildView
#synthesize textField= _textField;
#synthesize button = _button;
#synthesize requiredMarker = _requiredMarker;
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
[self setBackgroundColor:[UIColor colorWithRed:87.0f/255.0f green:153.0f/255.0f blue:191.0f/255.0f alpha:1.0f]];
self.textField = [[UITextField alloc] init];
[self.textField setFont:[UIFont fontWithName:#"Helvetica" size:14.0f]];
[self.textField setTextColor:[UIColor whiteColor]];
self.textField.text = #"Test text";
[self addSubview:self.textField];
self.button = [[UIButton alloc] init];
self.button.backgroundColor = [UIColor greenColor];
[self addSubview:self.button];
self.requiredMarker = [[UIImageView alloc] init];
[self.requiredMarker setBackgroundColor:[UIColor redColor]];
[self addSubview:self.requiredMarker];
}
return self;
}
-(void)layoutSubviews
{
CGSize hostSize = self.frame.size;
float length = 22;
self.textField.frame = CGRectMake(10, length, hostSize.width, hostSize.height-length);
self.button.frame = CGRectMake(0, 0, hostSize.width, length);
self.requiredMarker.frame = CGRectMake(0, 0, 4, length);
}
#end

how to do zooming image xcode?? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i have a problem. I have this code a zoom doesn't work.
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed:#"zoomimage.jpeg"];
CGRect yFrame = CGRectMake(0.0, 0.0, 320, 432);
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height)];
[imageView setImage:image];
scrollView = [[UIScrollView alloc] initWithFrame:yFrame];
[scrollView setScrollEnabled:YES];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setContentSize:CGSizeMake(image.size.width, image.size.height)];
scrollView.minimumZoomScale = 0.4;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:scrollView.minimumZoomScale];
[self.view addSubview:scrollView];
[self.view bringSubviewToFront:scrollView];
[scrollView addSubview:imageView];
}
First, set the delegate of scrollview to your self.
scrollView.delegate = self;
Now, Add this delegate method of UIScrollView in your class to enable zooming on the image.
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
Try this:
.h Controller:
#interface MyViewController : UIViewController{
IBOutlet UIScrollView *scrollView;
UIImageView *imageView;
}
#property (nonatomic, retain) UIScrollView *scrollView;
#property (nonatomic, retain) UIImageView *imageView;
.m Controller:
#synthesize scrollView, imageView;
-(UIView *)viewForZoomingInScrollView:(UIScrollVi­ew *)scrollView{
return imageView;
}
- (void)viewDidLoad
{
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"YourImageName.png"]];
self.imageView = tempImageView;
[tempImageView release];
scrollView.maximumZoomScale = 3.0;
scrollView.minimumZoomScale = 0.6;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
}
This code worked for me :)

1 UIViewController and 2 switching UIViews programmatically

I am bit stack with creating two UIViews which are switchable (each UIView has some of subviews). Since I do not IB, I wanna do that programmatically.
I have tried adding my subviews to my first UIView, then the same with second one and then switching to view like this.
if ([self.setView superview]) {
[self.setView removeFromSuperview];
[self.view addSubview:contentView];
self.navigationItem.title = #"BaseLine";
} else {
[self.contentView removeFromSuperview];
self.view = setView;
self.navigationItem.title = #"Settings";
}
but only thing which works correctly was "title" and nothing appeared on the UIViews. The UIView seems to be OK because when I use
self.view = contentView;
or
self.view = setView;
both showing subviews correctly.
Pls someone kick me to the right direction about this.
tnx
EDIT:
Also pointing the solution, perhaps something wrong with my initialization in the loadView
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
contentView = [[UIView alloc] initWithFrame:screenRect];
setView = [[UIView alloc] initWithFrame:screenRect];
self.view = contentView;
I tried to add it first [self. view add..] but the app crashed, so I used this
EDIT2
the root controller is UITableViewController..it is in navigation view and after choosing a cell this UIVieController (percView) with two UIVies is allocated
ShakeControl *percView = [[ShakeControl alloc] init];
[self.navigationController pushViewController:percView animated:YES];
[percView release];
EDIT3
switch is done by button, but it is fine done because I used that many times and it worked
Here is an example of a view controller that does what you want (I think). It is very basic, just switching between two views with different color backgrounds. But, you could further customize those subviews in the loadView method to display whatever you need.
The interface file:
#interface SwapViewController : UIViewController {
UIView *firstView;
UIView *secondView;
BOOL displayingFirstView;
UIButton *swapButton;
}
#property (nonatomic, retain) UIView *firstView;
#property (nonatomic, retain) UIView *secondView;
#property (nonatomic, retain) UIButton *swapButton;
- (void)swap;
#end
The implementation file:
#import "SwapViewController.h"
#implementation SwapViewController
#synthesize firstView;
#synthesize secondView;
#synthesize swapButton;
- (void)loadView
{
CGRect frame = [[UIScreen mainScreen] applicationFrame];
UIView *containerView = [[UIView alloc] initWithFrame:frame];
containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view = containerView;
[containerView release];
frame = self.view.bounds;
firstView = [[UIView alloc] initWithFrame:frame];
firstView.backgroundColor = [UIColor redColor];
secondView = [[UIView alloc] initWithFrame:frame];
secondView.backgroundColor = [UIColor blueColor];
self.swapButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.swapButton.frame = CGRectMake(10, 10, 100, 50);
[swapButton addTarget:self action:#selector(swap) forControlEvents:UIControlEventTouchUpInside];
displayingFirstView = YES;
[self.view addSubview:firstView];
[self.view addSubview:swapButton];
}
- (void)swap
{
if(displayingFirstView) {
[self.firstView removeFromSuperview];
[self.view addSubview:secondView];
displayingFirstView = NO;
} else {
[self.secondView removeFromSuperview];
[self.view addSubview:firstView];
displayingFirstView = YES;
}
[self.view bringSubviewToFront:self.swapButton];
}
#end
sample code;
BOOL firstview = YES; // First, I have added view1 as a subview;
if (firstview) {
[view1 removeFromSuperview];
[self.view addSubview:view2];
} else {
[view2 removeFromSuperview];
self.view addSubview:view1];
}