0

I have 2 ViewControllers : 1) MainViewController 2) WebViewController

and a segue with identifier ShowWeb.

MainViewController Contain Multiple NSObjects instances initiated from one class Name AppView: NSObject

inside each NSObject there is a UIButton that I want it to perform the segue of type show.

How to perform the segue?

sorry if my question is repeated but i searched yesterday for about 4 hours all the site and didn't find the exact manner, and to know I am using swift.

I Will explain more clearly:

class MainViewController: {

    var appviews = [AppView]()
override func viewDidLoad() {
    super.viewDidLoad()
        for i = 0; i < MyApps.count ; ++i{
            var appview =  AppView()
            appview = AppView(sourceView: AppsScrollView, AppIconURLString: AppPicture,AppName: "MyAppName", LoadedDescription:"Very Good Application", ObjectNo: i, LinkButtonString: "http://stackoverflow.com/posts/28288611")
            appviews.append(appview)
}
}
//
//  AppView.swift
//  Arabic Apps
//
//  Created by d-point on 3/24/1436 AH.
//  Copyright (c) 1436 e-Dever. All rights reserved.
//

import UIKit

class AppView: NSObject {

    var BackView = UIView()
    var AppIconView = UIImageView()
    var TitleLabel = UILabel()
    var DescriptionLabel = UILabel()
    var LinkIcon = UIImageView()
    var AppNo:Int!
    var DescriptionText:String!
    var AppTitleName:String!
    let originView:UIView!
    let LinkImage = UIImage(named: "HyperLink.png")
    var LinkButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
    let TitleHeight:CGFloat = 20.0
    let DescHeight:CGFloat = 40.0
    let BackViewHeight:CGFloat = 100.0
    let AppIconSize:CGFloat = 80.0
    let Inset:CGFloat = 10.0
    let BackgroundAlpha:CGFloat = 0.5
    let LinkButtonSize:CGFloat = 35.0
    var LinkButtonPath = String()
    var MyViewController = UIViewController()

    override init() {
        super.init  ()
    }

    init(sourceView:UIView, AppIconURLString: String? , AppName: String,LoadedDescription: String , ObjectNo: Int , LinkButtonString: NSString){
        super.init()

        originView = sourceView
        AppIconView.image = UIImage(named: "App-Icon-Copy.png")
        if var Icon = AppIconURLString {
        ImagesLoader.downloadImage(NSURL(string: Icon), handler: {image, error in
            self.AppIconView.image = image

        })}
        AppTitleName = AppName
        DescriptionText = LoadedDescription
        AppNo = ObjectNo
        LinkButtonPath = LinkButtonString
        setupAppView()
    }

     func setupAppView() {


        //SetupBackView
        BackView.frame = CGRectMake(Inset,
                                    Inset + (CGFloat(AppNo) * (BackViewHeight + 15)),
                                    originView.bounds.size.width - (Inset * 2),
                                    BackViewHeight)

        BackView.backgroundColor = UIColor.whiteColor().colorWithAlphaComponent(BackgroundAlpha)
        BackView.layer.cornerRadius = 20.0

        BackView.clipsToBounds = false
        originView.addSubview(BackView)

        //SetupAppIconView
        AppIconView.frame = CGRectMake(BackView.bounds.size.width - AppIconSize - Inset, Inset, AppIconSize, AppIconSize)
        AppIconView.layer.cornerRadius = 15.0
        AppIconView.layer.borderWidth = 2.0
        AppIconView.layer.borderColor = UIColor.whiteColor().CGColor

        AppIconView.clipsToBounds = true
        BackView.addSubview(AppIconView)


        //SetupTitleView

        TitleLabel.frame = CGRectMake(Inset, Inset, BackView.bounds.size.width - AppIconSize - Inset*6, TitleHeight)
        TitleLabel.text = AppTitleName
        TitleLabel.textColor = UIColor.blackColor()
        TitleLabel.textAlignment = NSTextAlignment.Center
        //TitleLabel.font = UIFont(name: TitleLabel.font.fontName, size: 18.0)
        TitleLabel.font = UIFont.boldSystemFontOfSize(16.0)
        BackView.addSubview(TitleLabel)

        //SetupDescriptionLabel
        DescriptionLabel.frame = CGRectMake(Inset, TitleHeight, BackView.bounds.size.width - AppIconSize - Inset*6, DescHeight)
        DescriptionLabel.text = DescriptionText
        DescriptionLabel.textColor = UIColor.blackColor()
        DescriptionLabel.textAlignment = NSTextAlignment.Justified
        BackView.addSubview(DescriptionLabel)


        //SetupButtonView
        //LinkButton
        LinkButton.frame = BackView.bounds
        LinkButton.addTarget(self, action:"GotoLinkURL:", forControlEvents:UIControlEvents.TouchUpInside)


        LinkButton.tag = AppNo
        BackView.addSubview(LinkButton)


    }
    func GotoLinkURL(sender:UIButton) {


        var mainviewcontroller:MainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MainView") as MainViewController

        mainviewcontroller.performSegueWithIdentifier("ShowWeb", sender: self)


    }




}
L A
  • 966
  • 11
  • 25
FamousMaxy
  • 686
  • 5
  • 21
  • Your question is a little bit unclear for me. Is every UIButton visible? Should the segue happen when the user taps the button? I don't really get what you want to do, but maybe you are looking for the "performSegueWithIdentifier" method. – beeef Feb 02 '15 at 23:35
  • Sorry If I didn't explain it very well. Yes all the buttons are visible and I want the segue happen when the button taps the segue. when i call performSeguewithIdentifier from a normal uibutton designed in the storyboard , it performs but not in the programmatically genereated NSObject that contains my UIButton – FamousMaxy Feb 02 '15 at 23:44
  • Could you post the lines of code you have? That would be very helpful I think. – beeef Feb 02 '15 at 23:47
  • You need to get a reference to the MainViewController that you have on screen, not create a new one with instantiateViewControllerWithIdentifier. – rdelmar Feb 03 '15 at 00:04
  • and how to do that? I read an answer like this but i didn't figured out how, Sorry. I think you will save my life. – FamousMaxy Feb 03 '15 at 00:05
  • Take a look at this answer: http://stackoverflow.com/a/26457259/1630618 – vacawama Feb 03 '15 at 00:07
  • I Saw this before and it is already done in my storyboard and I named the segue with "ShowWeb". didn't work. but observed somthing that I am doing println in MainViewController viewdidload and when performing the segue as in my code it shows the println resaults again like that is my MainViewController called again??? – FamousMaxy Feb 03 '15 at 00:13
  • Is it allowed to upload the whole project for review and getting help? – FamousMaxy Feb 03 '15 at 00:15
  • Since MainViewController creates the AppView objects you should probably use delegation. MainViewController can set itself as the delegate of the AppView objects, and in those objects you would call some protocol method which the view controller would implement (and do the segue in that implementation). – rdelmar Feb 03 '15 at 00:15
  • Thank you rdelmar and thank you Stackoverflow for this service. It finally worked as you mentioned rdelmar by delegation. Really I appreciate that. – FamousMaxy Feb 03 '15 at 00:41
  • rdelmar please post as an answer so I can mark it to be a correct answer because I am so thankful. – FamousMaxy Feb 21 '15 at 18:24

0 Answers0