How to load content into a WebView by touching a UIView - iphone

I Have a collection of views in a view controller. by touching one of those views I would like to load specific data, for ex. a webpage into a web view that is on the same view controller
How would you accomplish that?
Thank you in advance
Here is my code with does not want to work:
UIView *categoryTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 166 * counter
, 500, 20)];
UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 20)];
[categoryLabel setBackgroundColor:[UIColor clearColor]];
NSMutableArray *allCurrentNews = [[News alloc] allNewsFromCategory:cat.CategoryId];
categoryLabel.text = cat.Name;
categoryLabel.textColor = [UIColor whiteColor];
[categoryTitle addSubview:categoryLabel];
UIColor *myblack = [[UIColor alloc] initWithRed:0.14 green:0.14 blue:0.14 alpha:1];
UIColor *ligheterBlac = [[UIColor alloc] initWithRed:0.227 green:0.22 blue:0.22 alpha:1];
[categoryTitle setBackgroundColor:myblack];
UIScrollView *tempScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 166 * counter, 500, 166)];
UIColor *tempcolor = ligheterBlac;
tempScroll.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
tempScroll.layer.borderWidth = 0.5f;
int countnews = 0;
for (News *news in allCurrentNews)
{
UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
newsContainer.tag = countnews + 1;
[newsContainer addGestureRecognizer:recognizer];
//newsContainer.NewsId = news.NewsId;
LazyImageView *image = [[LazyImageView alloc] initWithURl:[NSURL URLWithString:news.Thumbnail]];
image.frame = CGRectMake(0, 0 , 156, 96);
UILabel *newsTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 96, 156, 30)];
newsTitle.backgroundColor = myblack;
newsTitle.numberOfLines = 2;
newsTitle.font = [UIFont systemFontOfSize:11];
newsTitle.text = news.Title;
newsTitle.textColor = [UIColor whiteColor];
newsTitle.textAlignment = UITextAlignmentCenter;
newsContainer.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
newsContainer.layer.borderWidth = 0.5f;
[newsContainer addSubview:image];
[newsContainer addSubview:newsTitle];
countnews ++;
[tempScroll setContentSize:CGSizeMake(allCurrentNews.count * 156, 96)];
[tempScroll addSubview:newsContainer];
//[image release];
}
[tempScroll setBackgroundColor: tempcolor];
[categories addSubview:tempScroll];
[categories addSubview:categoryTitle];
[tempcolor release];
[tempScroll release];
counter ++;
}
self.detailsView.layer.masksToBounds = NO;
self.detailsView.layer.shadowOffset = CGSizeMake(-10, 5);
self.detailsView.layer.shadowRadius = 5;
self.detailsView.layer.shadowOpacity = 0.3;
[self.view addSubview:categories];
[self.view addSubview:_detailsView];
[self.view addSubview:MainSubTitle];
[self.view addSubview:MainTitle];

Use the UITapGestureRecognizer.
- (void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:selector(processTap:)];
UIView *targetView = //assign the view you need
targetView.tag = 5;
[targetView addGestureRecognizer:recognizer];
}
- (void)processTap:(UITapGestureRecognizer *)recognizer {
UIView *view = recognizer.view;
if (view.tag == 5) {
}
}

Related

UITapGestureRecognizer not working when added for UIView Cannot detect the issue

UITapGestureRecognizer not working when added for UIView. Cannot detect the issue.
Note: I am not using it in ViewDidLoad. Please help me is solving the problem. Thanks in Advance.
-(void)setSegmentValues:(NSArray *) values
{
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tap1.numberOfTapsRequired = 1;
tap1.delegate=self;
[val1 addGestureRecognizer:tap1];
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
val1.backgroundColor = [UIColor magentaColor];
label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,70, 40)];
label1.backgroundColor = [UIColor yellowColor];
label1.text = [values objectAtIndex:0];
label1.textAlignment = UITextAlignmentCenter;
val1.userInteractionEnabled=YES;
[val1 addGestureRecognizer:tap1];
[val1 addSubview:label1];
[self addSubview:val1];
}
Other stuffs:
- (void)tapDetected:(UITapGestureRecognizer *)tapRecognizer
{
NSLog(#"success1");
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
Note: I've also added UIGestureRecognizerDelegate.
Just do like this.. You will get correct solution..
-(void)setSegmentValues:(NSArray *)values bgColor:(UIColor *)color
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(selectedIndexValue:)];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
viewOne = [[UIView alloc]initWithFrame:CGRectMake(40, 50, 80, 60)];
viewOne.backgroundColor = color;
viewOne.tag = 0;
UILabel *lblOne = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 60)];
lblOne.text = [values objectAtIndex:0];
lblOne.textAlignment = UITextAlignmentCenter;
lblOne.backgroundColor = [UIColor clearColor];
[viewOne addSubview:lblOne];
viewOne.userInteractionEnabled = YES;
[viewOne addGestureRecognizer:tap];
[self addSubview:viewOne];
}
-(void)selectedIndexValue:(UIGestureRecognizer *)gesture
{
int myViewTag = gesture.view.tag;
if (myViewTag == 0)
{
selectedIndex1 = 0;
self.lblValue.text = [valueArray objectAtIndex:myViewTag];
viewOne.backgroundColor = [UIColor colorWithRed:57.0f green:122.0f blue:255.0f alpha:1.0f];
viewTwo.backgroundColor = viewColor;
viewThree.backgroundColor = viewColor;
}
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
Is you control even going in "setSegmentValues:" method?
Make sure your method is being called from viewDidLoad or viewWillAppear or any IBAction.
this is error:
[val1 addGestureRecognizer:tap1];
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
write:
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
[val1 addGestureRecognizer:tap1];
You added a gesture to an UiView without alloc init.
Initialize the View before adding Gesture to it
Please use this code
-(void)setSegmentValues:(NSArray *) values
{
val1 = [[UIView alloc]initWithFrame:CGRectMake(40, 200, 70, 40)];
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapDetected:)];
tap1.numberOfTapsRequired = 1;
tap1.delegate=self;
[val1 addGestureRecognizer:tap1];
val1.backgroundColor = [UIColor magentaColor];
label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,70, 40)];
label1.backgroundColor = [UIColor yellowColor];
label1.text = [values objectAtIndex:0];
label1.textAlignment = UITextAlignmentCenter;
val1.userInteractionEnabled=YES;
[val1 addGestureRecognizer:tap1];
[val1 addSubview:label1];
[self addSubview:val1]
}

About IOS UIScrollView Sliding automatic will bounce back

My app's UIScrollView Sliding automatic will bounce back,what can i do?
I do not want!
my code below:
- (void)initScrollView {
// a page is the width of the scroll view
sv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 96, 320, 300)];
sv.contentSize = CGSizeMake(sv.frame.size.width * 3, sv.frame.size.height* 2);
sv.pagingEnabled = YES;
sv.clipsToBounds = YES;
sv.showsHorizontalScrollIndicator = NO;
sv.showsVerticalScrollIndicator = YES;
sv.scrollsToTop = NO;
sv.directionalLockEnabled = YES;
sv.delegate = self;
[self.view addSubview:sv];
currentPage = 0;
pageControl.numberOfPages = 3;
pageControl.currentPage = 0;
pageControl.backgroundColor = [UIColor clearColor];
[self createAllEmptyPagesForScrollView];
}
- (void)createAllEmptyPagesForScrollView {
tapView1 = [[UIView alloc]init];
tapView1.backgroundColor = [UIColor clearColor];
tapView1.frame = CGRectMake(320*0, 0, 320, sv.frame.size.height);
tapView2 = [[UIView alloc]init];
tapView2.backgroundColor = [UIColor clearColor];
tapView2.frame = CGRectMake(320*1, 0, 320, sv.frame.size.height);
tapView3 = [[UIView alloc]init];
tapView3.backgroundColor = [UIColor clearColor];
tapView3.frame = CGRectMake(320*2, 0, 320, sv.frame.size.height);
[sv addSubview:tapView1];
[sv addSubview:tapView2];
[sv addSubview:tapView3];
}
i konw why I slide up automatically bounce back,my code have problem!Not bove code!thanks everyone!

How can i create a gallery for displaying photos with the swipe gesture?

I have multiple photos in an NSArray and i want to display them with the swipe gesture in full size without using Three20 . so how can i do that?
Use a scrollView for accomplish that :
UIScrollView * scroller = [[[UIScrollView alloc] initWithFrame:CGTectMake(0, 0, 320, 480)] autorelease];
scroller.pagingEnabled = YES;
[self.view addSubview:scroller];
int x = 0;
for (int i = 0; i < [yourArray count]; i++){
UIImageView * myImageview = [[[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 320, 480)] autorelease];
[myImageview setImage:[yourArray objectAtIndex:i]];
[scroller addSubview:myView];
x += myView.frame.size.width;
}
[scroller setContentSize:CGSizeMake(x, 480)];
This Code works for me great:
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];

Subclassing MKPinAnnotationView

I'm creating my own annotation callout by subclassing MKPinAnnotationView. Everything works great, except for when I try to add buttons inside the new callout... the buttons are never called on touch... any idea why?
Thanks in advance!
Code is below:
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.frame = CGRectMake(0.0f, 0.0f, 284.0f, 245.0f);
self.backgroundColor = [UIColor clearColor];
UIImageView *callout_bkg = [[UIImageView alloc] init];
callout_bkg.image = [UIImage imageNamed:#"callout_bkg.png"];
callout_bkg.frame = CGRectMake(0, 0, 240, 110);
[self addSubview:callout_bkg];
titleLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 8.0f, 145.0f, 20.0f)];
titleLabel_.textColor = [UIColor whiteColor];
titleLabel_.textAlignment = UITextAlignmentLeft;
titleLabel_.backgroundColor = [UIColor clearColor];
titleLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:12];
[self addSubview:titleLabel_];
descLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 30.0f, 180.0f, 40.0f)];
descLabel_.textColor = [UIColor grayColor];
descLabel_.textAlignment = UITextAlignmentLeft;
descLabel_.backgroundColor = [UIColor clearColor];
descLabel_.numberOfLines = 15;
descLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:descLabel_];
communityLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(125.0f, 8.0f, 60.0f, 20.0f)];
communityLabel_.textColor = [UIColor whiteColor];
communityLabel_.textAlignment = UITextAlignmentRight;
communityLabel_.backgroundColor = [UIColor clearColor];
communityLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:communityLabel_];
typeLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(12.0f, 72.0f, 50.0f, 20.0f)];
typeLabel_.textColor = [UIColor whiteColor];
typeLabel_.textAlignment = UITextAlignmentLeft;
typeLabel_.backgroundColor = [UIColor clearColor];
typeLabel_.font = [UIFont fontWithName:#"Geogrotesque" size:10];
[self addSubview:typeLabel_];
facebook_share = [[UIButton alloc] initWithFrame:CGRectMake(200, 17, 29, 27)];
facebook_share.backgroundColor = [UIColor clearColor];
[facebook_share setBackgroundImage:[UIImage imageNamed:#"btn_annotation_share_fb.png"] forState:UIControlStateNormal];
[facebook_share addTarget:self action:#selector(calloutAccessoryTapped) forControlEvents:UIControlStateNormal];
[self addSubview:facebook_share];
twitter_share = [[UIButton alloc] initWithFrame:CGRectMake(200, 44, 29, 28)];
twitter_share.backgroundColor = [UIColor clearColor];
[twitter_share setBackgroundImage:[UIImage imageNamed:#"btn_annotation_share_twitter.png"] forState:UIControlStateNormal];
[twitter_share addTarget:self action:#selector(calloutAccessoryTapped) forControlEvents:UIControlStateNormal];
[self addSubview:twitter_share];
}
return self;
}
-(void)calloutAccessoryTapped {
NSLog(#"TEST!");
}
- (void)dealloc {
[facebook_share release];
[twitter_share release];
[community_ release], community_ = nil;
[communityLabel_ release], communityLabel_ = nil;
[type_ release], type_ = nil;
[typeLabel_ release], typeLabel_ = nil;
[desc_ release], desc_ = nil;
[descLabel_ release], descLabel_ = nil;
[title_ release], title_ = nil;
[titleLabel_ release], titleLabel_ = nil;
[super dealloc];
}
-(void)drawRect:(CGRect)rect {
[super drawRect:rect];
titleLabel_.text = self.type;
descLabel_.text = self.desc;
communityLabel_.text = self.community;
typeLabel_.text = self.title;
[facebook_share addTarget:self action:#selector(test:) forControlEvents:UIControlStateNormal];
}
try with :
facebook_share = [UIButton buttonWithType:UIButtonTypeCustom];
facebook_share.frame = yourFrame;

Thumbnail grid of images

How does one create a dynamic thumbnail grid?
How is a grid created with thumbnails of images like the photo app on iphone?
How are these spaces arranged dynamically? e.g. adding and removing thumbnails.
this is my simple grid view app
- (void)viewDidAppear:(BOOL)animated {
[[Grid_ViewAppDelegate sharedAppDelegate] hideLoadingView];
temp = temp+1;
if (temp ==1){
NSLog(#"temp:%d",temp);
int n = 20; //Numbers of array;
NSArray *t = [[NSArray alloc] initWithObjects:#"calpie.gif",#"chrysler_electric.png",#"chrysler_fiat_cap.gif",#"cleanup.gif",#"ecylindri.gif",#"elect08.png",#"globe.gif",#"heat.gif",#"insur.gif"
,#"jobs.gif",#"office.gif",#"opec1.gif",#"orng_cap.gif",#"poll.gif",#"pollen.gif",#"purbar.gif",#"robinson.gif",#"robslink_cap.gif",#"scot_cap.gif",#"shoes.gif",nil];
myScrollView.contentSize = CGSizeMake(320, 460+n*2);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 1;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
NSString *mxiURL = #"http://meta-x.com/biflash/images/";
int i =0,i1=0;
while(i<n){
int yy = 4 +i1*79;
for(int j=0; j<4;j++){
if (i>=n) break;
CGRect rect;
rect = CGRectMake(4+79*j, yy, 75, 75);
UIButton *button=[[UIButton alloc] initWithFrame:rect];
[button setFrame:rect];
NSString *nn = [mxiURL stringByAppendingString:[t objectAtIndex:i]];
UIImage *buttonImageNormal=[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: nn]]];
[button setBackgroundImage:buttonImageNormal forState:UIControlStateNormal];
button.tag =i;
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside ];
//[self.view addSubview:button];
[myScrollView addSubview:button];
//[buttonImageNormal release];
[button release];
i++;
//
}
i1 = i1+1;
//i=i+4;
}
}
}
in AppDelegate
+ (Grid_ViewAppDelegate *)sharedAppDelegate
{
return (Grid_ViewAppDelegate *)[UIApplication sharedApplication].delegate;
}
- (void)showLoadingView
{
if (loadingView == nil)
{
loadingView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 480.0)];
loadingView.opaque = NO;
loadingView.backgroundColor = [UIColor grayColor];
loadingView.alpha = 0.5;
UIActivityIndicatorView *spinningWheel = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(142.0, 222.0, 37.0, 37.0)];
[spinningWheel startAnimating];
spinningWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loadingView addSubview:spinningWheel];
[spinningWheel release];
CGRect label = CGRectMake(142.0f, 255.0f, 71.0f, 20.0f);
lblLoading = [[UILabel alloc] initWithFrame:label];
lblLoading.textColor = [UIColor whiteColor];
lblLoading.font = [UIFont boldSystemFontOfSize:14];
// UILabel
lblLoading.backgroundColor =[UIColor clearColor];
[lblLoading setText:#"Loading..."];
//[lblLoading setTextAlignment:UITextAlignmentCenter];
[loadingView addSubview:lblLoading];
}
[window addSubview:loadingView];
}
- (void)hideLoadingView
{
[loadingView removeFromSuperview];
}
definitely if u apply this logic. u get succeed