I am a new coder in iOS. When I was trying to code ScrollView with paging, there is an issue to be fixed. I really don't know what is the problem in my code. I already set pageEnable, and set the page number with 4 current 0. In xib.file, I created 4 views, make scrollview connect "delegate" to "file's owner". My reference link http://www.iosdevnotes.com/2011/03/uiscrollview-paging/
When I run the app, the view cannot be paged.
Thank you for your help.
Here is my code:
////////// I think there is a problem in the for loop in viewDidLoad.
import
#interface ThirdViewController : UIViewController {
IBOutlet UIScrollView *scrollView;
IBOutlet UIPageControl *pageControl;
IBOutlet UIView *contentOne;
IBOutlet UIView *contentTwo;
IBOutlet UIView *contentThree;
IBOutlet UIView *contentFour;
}
- (IBAction)pageValueChange:(id)sender;
#property(nonatomic,strong) IBOutlet UIScrollView *scrollView;
#property(nonatomic,strong) IBOutlet UIView *contentOne;
#property(nonatomic,strong) IBOutlet UIView *contentTwo;
#property(nonatomic,strong) IBOutlet UIView *contentThree;
#property(nonatomic,strong) IBOutlet UIView *contentFour;
#property(nonatomic,strong) IBOutlet UIPageControl *pageControl;
#property(nonatomic,strong) NSArray *viewArray;
#end
#import "ThirdViewController.h"
#interface ThirdViewController ()<UIScrollViewDelegate>
#end
#implementation ThirdViewController
#synthesize scrollView,contentOne, contentTwo,contentThree,contentFour,pageControl,viewArray;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Profile", #"Third");// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
viewArray = [[NSArray alloc]initWithObjects:#"contentOne",#"contentTwo",#"contentThree", #"contentFour",nil];
for (int i =0; i < [viewArray count]; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i ;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview =[[UIView alloc]initWithFrame:frame];
[self.scrollView addSubview: subview]; //////how to pass the array object to subview
[subview removeFromSuperview];
}
// Do any additional setup after loading the view from its nib.
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width *[viewArray count], scrollView.frame.size.height);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)dismissKeyboard :(id) sender{
[sender resignFirstResponder];
}
- (IBAction)pageValueChange:(id)sender{
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
}
#pragma mark -UIScrollView Delegate
-(void)scrollViewDidScroll:(UIScrollView *)sender{
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x-pageWidth/2)/pageWidth)+1;
self.pageControl.currentPage = page;
}
#end
Did you put a breakpoint next to the line it crashed on?
Related
I have a custom view about alertView.
But when I init my alertView, it seem not set my awakeFromNib value of label.
What's wrong about my code?
I can't figure out this issue.
Thanks.
AlertView.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
#interface AlertView : UIView
#property (weak, nonatomic) IBOutlet UILabel *labTitle;
#property (weak, nonatomic) IBOutlet UILabel *labMessage;
#property (weak, nonatomic) IBOutlet UIButton *btnCancel;
#property (weak, nonatomic) IBOutlet UIButton *btnConfirm;
#end
AlertView.m
#import "AlertView.h"
#interface AlertView ()
#property (weak, nonatomic) IBOutlet UIView *headerView;
#end
#implementation AlertView
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(!self){
return nil;
}
self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
owner:self
options:nil] firstObject];
self.frame = frame;
return self;
}
- (void)awakeFromNib{
[super awakeFromNib];
// custom label text not show.
self.labTitle.text = #"123";
self.labMessage.text = #"456";
[self.btnCancel setTitle:#"789"; forState:UIControlStateNormal];
[self.btnConfirm setTitle:#"111"; forState:UIControlStateNormal];
self.btnCancel.layer.cornerRadius = 5.f;
self.btnConfirm.layer.cornerRadius = 5.f;
self.headerView.layer.cornerRadius = 10;
}
#end
Viewcontroller.m
#interface Viewcontroller ()
#property AlertView * alertView;
#end
#implementation Viewcontroller
- (void)viewDidLoad {
[self popupView];
}
- (void)popupView{
CGRect viewSize = CGRectMake(16, 100, [[UIScreen mainScreen] bounds].size.width - 16 * 2, self.height);
self.alertView = [[ResultAlertView alloc] initWithFrame:viewSize];
self.alertView.layer.cornerRadius = 10;
[self SetShadowForView:self.alertView];
[self.alertView.btnConfirm addTarget:self action:#selector(cancelStartUsingView:) forControlEvents:UIControlEventTouchUpInside];
[self.alertView.btnCancel addTarget:self action:#selector(cancelStartUsingView:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.alertView];
}
- (IBAction)cancelStartUsingView:(UIButton *)sender{
//Btn also not work.
NSLog(#"123");
}
#end
you can use:
- (void)layoutSubviews {
[super layoutSubviews];
self.labTitle.text = #"123";
self.labMessage.text = #"456";
[self.btnCancel setTitle:#"789" forState:UIControlStateNormal];
[self.btnConfirm setTitle:#"111"forState:UIControlStateNormal];
self.btnCancel.layer.cornerRadius = 5.f;
self.btnConfirm.layer.cornerRadius = 5.f;
self.headerView.layer.cornerRadius = 10;
}
and call
- (void)viewDidLoad {
dispatch_async(dispatch_get_main_queue(), ^{
[self popupView];
});
}
I am using following code to set the text of a UILabel in SViewController, From another ViewController FViewController
SViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SViewController"];
[vc setTextForLabel:[NSNumber numberWithInt:1];];
[self.navigationController pushViewController:vc animated:YES];
The setTextForLabel method:
-(void) setTextForLabel: (NSNumber *)text {
textLabel = [[UILabel alloc] init];
[textLabel setText:[NSString stringWithFormat:#"%#",[text stringValue]]];
NSLog(#"The text is %#", textLabel.text);
}
I have declared the property for textLabel in .h file is as follows:
#property (nonatomic, retain) IBOutlet UILabel *textLabel;
And I have also set the IBOutlet in storyboard.
Now in console I see "The text is 1", but the UI is not showing anything!
What am I doing wrong?
remove this line
textLabel = [[UILabel alloc] init];
Also, you need to link the UILabel on interface to your #property (nonatomic, retain) IBOutlet UILabel *textLabel;
Or you may add this label manually to view
textLabel = [[UILabel alloc] init];
[textLabel setText:[NSString stringWithFormat:#"%#",[text stringValue]]];
[self.view addSubView:textLabel];
First of all go put
[vc setTextForLabel:[NSNumber numberWithInt:1]];
after you push the vc.
In your xib/storyboard if your label has something written in it, remove it, leave it blank.
If that doesn't work ... leave the setText method before the push one (as you wrote it initialy) and create a NSNumber member that will hold your value and apply it in viewDidAppear like so
-(void) setTextForLabel: (NSNumber *)text{
numberValue = text; // declared in .h as NSNumber *numberValue (only make it a property if you need acces to it from outside your class)
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; //this is important
[textLabel setText:[NSString stringWithFormat:#"%#",[numberValue stringValue]]];
}
Your problem is linked to the view lifecycle of SViewController. When you call [.. setTextForLabel] you don't know if the controller has loaded its views (And thus your UILabel have a good chance of not being created yet)
What you should do is use a NSString property on your controller, set it with the desire text and assigned it to the UILabel in the viewDidLoad of SViewController.
in SViewController.h
#propery (strong, nonatomic) NSString *textToDisplay;
in SViewController.m
-(void)viewDidLoad
{
[super viewDidLoad];
// Here you know all Outlet are loaded and connected
textLabel.text = textToDisplay
}
I did like this
#import "FViewController.h"
#import "SViewController.h"
#interface FViewController ()
#end
#implementation FViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)do:(id)sender
{
SViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"SViewController"];
[self.navigationController pushViewController:vc animated:YES];
[vc setTextForLabel:[NSNumber numberWithInt:1]];
}
// in SViewController.h
#import <UIKit/UIKit.h>
#interface SViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *label;
-(void) setTextForLabel: (NSNumber *)text;
#end
// SViewController.m file
#import "SViewController.h"
#interface SViewController ()
#end
#implementation SViewController
#synthesize label;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void) setTextForLabel: (NSNumber *)text{
// label.text = [text stringValue];
NSLog(#"%#",label);
[label setText:[text stringValue]];
}
#end
also check your label properties in storyboard are properly set
I have tab bar based app which works great. But I am wanting to use the UIPagecontrol to allow the user to swipe between views.
I have been using the tutorial http://www.samsurge.com/p/blog-page.html to achieve this for my app. But the difference between the tutorial and my app is that my app is based in tab bar system.
The UIscrollview is based on the first tab option with the class pageViewController and the connecting sub views are in IntroViewController.
The storyboard layout looks like this.
http://threepointdesign.co.uk/img2.png
The error generating is
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array'
Headers for both classes
#import <UIKit/UIKit.h>
#interface simpleMain : UIViewController <UIScrollViewDelegate>
#property (nonatomic, strong) IBOutlet UIScrollView *scrollView;
#property (nonatomic, strong) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage:(id)sender;
#end
#import "simpleMain.h"
#interface simpleMain ()
#property (assign) BOOL pageControlUsed;
#property (assign) NSUInteger page;
#property (assign) BOOL rotating;
- (void)loadScrollViewWithPage:(int)page;
#end
#implementation simpleMain
#synthesize scrollView;
#synthesize pageControl;
#synthesize pageControlUsed = _pageControlUsed;
#synthesize page = _page;
#synthesize rotating = _rotating;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.scrollView setPagingEnabled:YES];
[self.scrollView setScrollEnabled:YES];
[self.scrollView setShowsHorizontalScrollIndicator:NO];
[self.scrollView setShowsVerticalScrollIndicator:NO];
[self.scrollView setDelegate:self];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
for (NSUInteger i =0; i < [self.childViewControllers count]; i++) {
[self loadScrollViewWithPage:i];
}
self.pageControl.currentPage = 0;
_page = 0;
[self.pageControl setNumberOfPages:[self.childViewControllers count]];
UIViewController *viewController = [self.childViewControllers objectAtIndex:self.pageControl.currentPage];
if (viewController.view.superview != nil) {
[viewController viewWillAppear:animated];
}
self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * [self.childViewControllers count], scrollView.frame.size.height);
}
- (void)loadScrollViewWithPage:(int)page {
if (page < 0)
return;
if (page >= [self.childViewControllers count])
return;
// replace the placeholder if necessary
UIViewController *controller = [self.childViewControllers objectAtIndex:page];
if (controller == nil) {
return;
}
// add the controller's view to the scroll view
if (controller.view.superview == nil) {
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[self.scrollView addSubview:controller.view];
}
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
_pageControlUsed = NO;
}
#end
#import "simpleMain.h"
#interface miniViewController : simpleMain {
}
#property (strong, nonatomic) IBOutlet UIView *View1;
#property (strong, nonatomic) IBOutlet UIView *View2;
#property (strong, nonatomic) IBOutlet UIView *View3;
#end
#import "MiniViewController.h"
#interface miniViewController ()
#end
#implementation miniViewController
#synthesize View1;
#synthesize View2;
#synthesize View3;
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View1"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View2"]];
[self addChildViewController:[self.storyboard instantiateViewControllerWithIdentifier:#"View3"]];
}
#end
(I know the error reads, I am trying to find an element inside and empty array. I just don't know where the array is coming from and what connection it has to this setup).
Any help would be great.
From what I see in your storyboard and in your code, the pageViewController doesn't have any childViewControllers when the viewWillAppear is called. So that's causing the crash.
Ditched this tutorial as I found it had to many flaws. Problem solved.
I have a tabled application built with storyboards, so I would like to add a paging possibility for the items in table. I used this tutorial:
So I added the new view to the storyboard and the UIScrollView on the view. Than made a property UIScrollView* scrollView; and make the relation scrollView - ECOMClPanelPagingViewController in storyboard. (ECOMClPanelPagingViewController is a UIViewController class for my view.
.h
#import <UIKit/UIKit.h>
#interface ECOMClPanelPagingViewController : UIViewController
{
UIScrollView *scrollView;
}
#property (nonatomic, retain) IBOutlet UIScrollView* scrollView;
#end
.m
#import "ECOMClPanelPagingViewController.h"
#interface ECOMClPanelPagingViewController ()
#end
#implementation ECOMClPanelPagingViewController
#synthesize scrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * colors.count, self.scrollView.frame.size.height);
}
- (void)viewDidUnload
{
self.scrollView = nil;
}
- (void)dealloc
{
[scrollView release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
So I see only the white background with navigation controller.. Who can help me with it?
Just need to uncheck the Use Autolayout option
I am trying to add Custom UIViews ( Graphs drawn using Core Plot ) as the elements in iCarousel, but I dont see the Graphs being drawn. Here is my header file :
#interface MTViewController : UIViewController <iCarouselDataSource, iCarouselDelegate,
CPTBarPlotDataSource, CPTBarPlotDelegate>
#property (strong, nonatomic) IBOutlet iCarousel *aCarousel;
#property (strong, nonatomic) IBOutlet UILabel *label;
#property (nonatomic) BOOL wrap;
#property (strong, nonatomic) IBOutlet CPTGraphHostingView *graphView;
#property (nonatomic, retain) NSMutableArray *data;
#property (strong, nonatomic) NSMutableArray *animals;
#property (nonatomic, retain) CPTXYGraph *graph;
#property (strong, nonatomic) NSMutableArray *graphs;
#property (strong, nonatomic) NSMutableArray *descriptions;
#end
Implementation file is :
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
//set up carousel data
wrap = NO;
self.graphs = [NSMutableArray arrayWithObjects: self.graphView,nil];
}
return self;
}
- (void)viewDidLoad
{
aCarousel.type = iCarouselTypeCoverFlow2;
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setACarousel:nil];
[self setLabel:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#pragma - mark iCarousel Delegate
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
return [self.graphs count];
}
- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
// limit the number of item views loaded concurrently (for performance)
return 1;
}
- (CPTGraphHostingView*)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(CPTGraphHostingView *)view
{
// create a numbered view
// view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[animals objectAtIndex:index]]];
// return view;
CGSize size = self.graphView.frame.size;
float width = size.width;
CPTGraphHostingView *currentGraph ;
for (int i = 0; i < self.graphs.count; i++) {
CGRect frame;
frame.origin.x = width * i;
frame.origin.y = 0;
frame.size = size;
currentGraph = [graphs objectAtIndex:i];
currentGraph.frame = frame;
[self generateBarPlot:frame];
}
return currentGraph;
}
- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
return 1;
}
- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
// usually this should be slightly wider than the item views
return 240;
}
- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
return self.wrap;
}
- (void)carouselDidScroll:(iCarousel *)carousel
{
//[label setText:[NSString stringWithFormat:#"%#", [descriptions objectAtIndex:carousel.currentItemIndex]]];
[label setText:[NSString stringWithFormat:#"Testing"]];
}
.... followed by methods to generate graph
Any help will be deeply appreciated.
Thanks for reading.
I solved it by casting the Custom View to UIView.