Can't attach a UIScrollView Paging in storyboards iOS - ios

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

Related

subclass of UITextView not editable

I create a class named SWTextView and it inherits from UITextView. It does nothing but add a placeholder label. When I initialize a SWTextView and put it in a UITableViewCell, it's not editable even when the editable property of the SWTextView is YES. Does anyone have met the same problem?
The SWTextView is as follow:
#import <UIKit/UIKit.h>
#interface SWTextView : UITextView
- (void)setPlaceholder:(NSString *)placeholder;
#end
#interface SWTextView ()
#property(nonatomic, strong) UILabel *placeholderLabel;
#end
#implementation SWTextView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self = [super initWithFrame:frame]) {
_placeholderLabel = [[UILabel alloc] init];
_placeholderLabel.textColor = [UIColor grayColor];
[self addSubview:_placeholderLabel];
self.font = [UIFont systemFontOfSize:12];
}
return self;
}
- (void)setFont:(UIFont *)font
{
[super setFont:font];
_placeholderLabel.font = font;
}
- (void)setPlaceholder:(NSString *)placeholder
{
_placeholderLabel.text = placeholder;
}
- (void)layoutSubviews
{
[super layoutSubviews];
[_placeholderLabel sizeToFit];
}
#end

Fetching Images from Parse in a Detail ScrollView

Ok, I really tried looking for an example or tutorial in how to achieve what I'm looking for, but haven't had any luck.
I have a PFQueryTableView that passes data to a DetailView (all good there).
My DetailView is a Horizontal ScrollView that gets images from the cell clicked. And here comes my problem: I manage to get the data to pass to the DetailView but I don't know how to set up the images in the ScrollView. Can anyone please send me on the direction of a tutorial or could help me via this question?
Here is my code: (Obviously I'm missing the section where you set up the images to be viewed in the ScrollView.)
BellezaTableViewController.m
#import "BellezaTableViewController.h"
#import "BellezaDetailViewController.h"
#import "BellezaView.h"
#interface BellezaTableViewController ()
#end
#implementation BellezaTableViewController {
}
- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
self.parseClassName = #"BellezaView";
self.textKey = #"cellTitle";
self.textKey = #"descriptionTitle";
self.pullToRefreshEnabled = YES;
self.paginationEnabled = NO;
self.loadingViewEnabled = YES;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (PFQuery *)queryForTable{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *simpleTableIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
UILabel *cellTitle = (UILabel*) [cell viewWithTag:101];
cellTitle.text = [object objectForKey:#"cellTitle"];
UILabel *descriptionTitle = (UILabel*) [cell viewWithTag:102];
descriptionTitle.text = [object objectForKey:#"descriptionTitle"];
return cell;
}
- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showBellezaDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BellezaDetailViewController *destViewController = segue.destinationViewController;
PFObject *object = [self.objects objectAtIndex:indexPath.row];
BellezaView *bellezaView = [[BellezaView alloc] init];
bellezaView.cellTitle = [object objectForKey:#"cellTitle"];
bellezaView.descriptionTitle = [object objectForKey:#"descriptionTitle"];
bellezaView.image_1 = [object objectForKey:#"image_1"];
destViewController.bellezaView = bellezaView;
}
}
#end
BellezaTableViewController.h
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface BellezaTableViewController : PFQueryTableViewController
#end
DetailViewController.m
#import "BellezaDetailViewController.h"
#import "BellezaView.h"
#interface BellezaDetailViewController ()
#end
#implementation BellezaDetailViewController
#synthesize lookPhoto, bellezaView, activityIndicator, scrollView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];{
[activityIndicator startAnimating];
[activityIndicator performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:60];
[scrollView setAlwaysBounceHorizontal:YES];
[scrollView setAlwaysBounceVertical:NO];
[scrollView setPagingEnabled:YES];
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++){
This would be the section I do not know how to handle, and can't find any examples to follow through. Should I use an array? If so, how do I retrieve the data if I should have passed it from the PFQueryTable? I found some examples that get images like this code:
image.image = [UIImage imageNamed: [NSString stringWithFormat:#"image_%d", i+1]];
but my problem is that my images have to be fetched by parse. So how do I do that? Please help!
lookPhoto.file = bellezaView.image_1;
[lookPhoto loadInBackground];
[scrollView addSubview:lookPhoto];
}
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
}
}
- (void)viewDidUnload {
[self setLookPhoto:nil];
[super viewDidUnload];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#import "BellezaView.h"
#import <Parse/Parse.h>
#interface BellezaDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet PFImageView *lookPhoto;
#property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;
#property (nonatomic, strong) BellezaView *bellezaView;
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#end
BellezaView.m
#import "BellezaView.h"
#implementation BellezaView
#synthesize cellTitle, descriptionTitle, image_1;
#end
BellezaView.h
#import <Foundation/Foundation.h>
#import <Parse/Parse.h>
#interface BellezaView : NSObject
#property (nonatomic, strong) NSString *cellTitle;
#property (nonatomic, strong) NSString *descriptionTitle;
#property (nonatomic, strong) PFFile *image_1;
#end
Thanks in advance!
If you have a link to the image as a property in your PFObject you can do something like
PFFile* imageFile = [object objectForKey:#"imageFile"];
UIImage* img = nil;
if (startImage != nil)
{
img = [[UIImage alloc] initWithData:[imgFile getData]];
}
The property "imageFile" is link to the image in Parse
Keep in mind that this makes a network call and so you should not do this on the main thread
Found a way, Here is my code for the DetailViewController.m file:
Hope it helps anyone in need! :)
- (void)viewDidLoad {
[super viewDidLoad];
//Do any additional setup after loading the view.
[activityIndicator startAnimating];
[activityIndicator performSelector:#selector(removeFromSuperview) withObject:nil afterDelay:60];
scrollView.pagingEnabled = YES;
[UIView animateWithDuration:20 animations:^{ScrollNext.alpha = 0.0;}];
NSInteger numberOfViews = 10;
for (int i = 0; i < numberOfViews; i++) {
PFImageView *lookView1 = [[PFImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
lookView1.image = [UIImage imageNamed:#"Lading.jpg"];
lookView1.file = bellezaView.image_1;
[scrollView addSubview:lookView1];
[lookView1 loadInBackground:^(UIImage *image, NSError *error) {
if (!error) {
lookView1.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);
lookView1.contentMode = UIViewContentModeScaleAspectFit;
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
}
}];
PFImageView *lookView2 = [[PFImageView alloc] init];
lookView2.frame = CGRectMake(lookView1.frame.size.width*1, 0, 320, 500);
lookView2.file = bellezaView.image_2;
[scrollView addSubview:lookView2];
[lookView2 loadInBackground:^(UIImage *image, NSError *error) {
if (!error) {
lookView2.contentMode = UIViewContentModeScaleAspectFit;
lookView2.frame = CGRectMake(lookView1.frame.size.width*1, 0, self.view.frame.size.width, self.view.frame.size.height);
}
}];
PFImageView *lookView3 = [[PFImageView alloc] init];
lookView3.frame = CGRectMake(lookView1.frame.size.width*2, 0, 320, 500);
lookView3.file = bellezaView.image_3;
[scrollView addSubview:lookView3];
[lookView3 loadInBackground:^(UIImage *image, NSError *error) {
if (!error) {
lookView3.contentMode = UIViewContentModeScaleAspectFit;
lookView3.frame = CGRectMake(lookView1.frame.size.width*2, 0, self.view.frame.size.width, self.view.frame.size.height);
}
}];
//Insert Another View
}
}

PageControl&ScrollView Combination

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?

Classical MVC in Cocoa

I'm trying to structure some view-code out of the controller (obviously without using a nib). Therefore, I tried a simple example, but for some reason, I can't add the target to the button in the controller, rest is fine. Here's what I'm trying:
Controller.h:
#import <UIKit/UIKit.h>
#import "IndexView.h"
#interface IndexController : UIViewController
{
}
#property (nonatomic) IndexView *contentView;
#end
Controller.m
#import "IndexController.h"
#import "IndexView.h"
#interface IndexController ()
#end
#implementation IndexController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.contentView = [[IndexView alloc]init];
[self.view addSubview:self.contentView];
[self connectUIElements];
}
return self;
}
- (void) connectUIElements
{
[self.contentView.testButton addTarget:self action:#selector(testButtonClicked) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark --
#pragma mark UIHandler
- (void) testButtonClicked
{
NSLog(#"testbutton clicked");
}
#end
View.h
#import <UIKit/UIKit.h>
#interface IndexView : UIView
#property (nonatomic, strong) UIButton *testButton;
#end
View.m
#import "IndexView.h"
#implementation IndexView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setUpView];
}
return self;
}
- (id) init
{
self = [super init];
if (self) {
[self setUpView];
}
return self;
}
- (void) setUpView
{
[self setBackgroundColor:[UIColor whiteColor]];
self.testButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 50, 100, 50)];
self.testButton.backgroundColor = [UIColor greenColor];
[self.testButton setTitle:#"hello, world" forState:UIControlStateNormal];
[self addSubview:self.testButton];
}
#end
I'm just exploring possibilities to get closer to a classical MVC pattern and a little farther away from Apples mediator-interpretation. Any idea what's wrong?
You have to create your view using initWithFrame:, which is the default initializer for UIView. (The reason that is does not work with a simple init - but I am guessing here! - might be that the views size is zero and therefore the touch events do not work.)

Passing data From uiview to uiviewcontroller

after try 2 days and i'm losing my mind now... the main problem is simple.. i've button in uiview,, and i want to call method on uiviewcontroller ... here's the code
Uiview call headerView.h
#class HeaderView;
#protocol headerViewDelegate
- (void)tes:(HeaderView *)tes ratingDidChange:(float)rating;
#end
#interface HeaderView : UIView {
UIButton *test;
}
#property (nonatomic,strong) UIButton *test;
#property (assign) id <headerViewDelegate> delegate;
#end
and the Headerview.M
#import "HeaderView.h"
#import <QuartzCore/QuartzCore.h>
#implementation HeaderView
#synthesize test;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame]; // not needed - thanks ddickison
if (self) {
test=[UIButton buttonWithType:UIButtonTypeRoundedRect];
test.frame=CGRectMake(30, 10, 100,40);
[test setTitle:#"Kampret" forState:UIControlStateNormal];
[test addTarget:self action:#selector(testdata:) forControlEvents:UIControlEventTouchUpInside];
}
[self addSubview:test];
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
}
return self;
}
- (void)dealloc {
[super dealloc];
}
-(void)layoutSubviews{
self.backgroundColor=[UIColor whiteColor];
[self release]; // release object before reassignment to avoid leak - thanks ddickison
//self = [nib objectAtIndex:0];
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOffset = CGSizeMake(1.0, 1.0);
self.layer.shadowOpacity = 0.40;
}
-(void)testdata:(id)sender{
[self.delegate tes:self ratingDidChange:1.1f];
}
#end
viewcontroller.h
#import <UIKit/UIKit.h>
#import "HeaderView.h"
#interface ReusableTableHeadersViewController : UITableViewController <headerViewDelegate> {
HeaderView *header;
}
#property(nonatomic,strong) HeaderView *header;
#end
viewcontroller.m
#import "viewcontroller.h"
#implementation ReusableTableHeadersViewController
#synthesize header;
#synthesize tblData, data;
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
header = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
[self.view addSubview:header];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[super dealloc];
}
-(void)tes:(HeaderView *)tes ratingDidChange:(float)rating{
NSLog(#"passing data==========%f",rating);
}
#end
i want to log the rating :
NSLog(#"passing data==========%f",rating);
from method:
-(void)testdata:(id)sender{
[self.delegate tes:self ratingDidChange:1.1f];
}
but it show nothing... why? because i use initwithframe?
dont blame me... i'm totally newbie here :)
You have not set delegate of headerview as view controller.
- (void)viewDidLoad {
[super viewDidLoad];
header = [[HeaderView alloc] initWithFrame:CGRectMake(0, 0, 320, 120)];
header.delegate = self;
[self.view addSubview:header];
}

Resources